Comments (9)
You can just use ToMethod
instead, which is transient.
from zenject-2019.
Oh wait, you're right, only BinderGeneric
has ToMethod surprisingly. I'll fix that.
from zenject-2019.
@svermeulen Thanks! I looked at the code inside BinderUntyped
and there only seem to be singleton methods so the fix doesn't look that simple.
from zenject-2019.
Ok I pushed a change that adds that method. I tested it in the unit tests but will wait to hear from you whether that solves your problem before closing this item
from zenject-2019.
I am having a small problem that didn't happen in ToSingleMethod
, in the new extension method
public static void ToPrefab(this BinderUntyped binder, String prefabLocation, Type type)
{
binder.ToMethod((ctx) => {
var obj = Resources.Load(prefabLocation) as GameObject;
return ctx.Container.InstantiatePrefabForComponent(type, obj) as MonoBehaviour;
});
}
TContract
in ToMethod<TContract>
gets set as MonoBehaviour
because its the only sensible type I can cast to. However, the actual type of obj
is type
(obj.GetType() == type
) and that type is known to extend MonoBehaviour
.
So Zenject complains
ZenjectBindException: Invalid type given during bind command. Expected type 'UnityEngine.MonoBehaviour' to derive from type
I actually commented your code (the part that check types) so it didn't throw that exception but I got
ZenjectException: Assert hit!
ModestTree.Assert.Throw (System.String message) (at Assets/Lib/Zenject/Internal/Assert.cs:195)
ModestTree.Assert.That (Boolean condition) (at Assets/Lib/Zenject/Internal/Assert.cs:29)
Zenject.MethodProvider`1[UnityEngine.MonoBehaviour].GetInstance (Zenject.InjectContext context) (at Assets/Lib/Zenject/Main/Scripts/Providers/MethodProvider.cs:25)
Zenject.DiContainer.Resolve (Zenject.InjectContext context) (at Assets/Lib/Zenject/Main/Scripts/Main/DiContainer.cs:578)
Zenject.DiContainer.Resolve (System.Type contractType) (at Assets/Lib/Zenject/Main/Scripts/Main/DiContainer.cs:1175)
I actually need something that binds the method type as a parameter like this
public static void ToPrefab(this BinderUntyped binder, String prefabLocation, Type type)
{
//Pass "type" into ToMethod
binder.ToMethod(type, (ctx) => {
var obj = Resources.Load(prefabLocation) as GameObject;
return ctx.Container.InstantiatePrefabForComponent(type, obj) as MonoBehaviour;
});
}
from zenject-2019.
If passing type
as a parameter is not possible, is the exception I get when I comment your code due to the same exception as yesterday (something about not having a CompositionRoot
)?
from zenject-2019.
Ok I added a non-generic version of ToMethod, try that. What errors are you getting now?
from zenject-2019.
Works!!!!!!!!!!!!!!!!!!!!!!!!
Thanks man. Now I am able to define Routes like this with my framework
[Route("some/route")]
public class SomeRouteView : MonoBehaviour
{
[PostInject]
public void PostConstruct(...) {
...
}
}
and start and configure like this
public class Startup : MVCApp
{
public override void Configure(IApplication app, DiContainer container)
{
//Set view dependencies
}
public override void Init(IRouter router)
{
router.GoTo(
url: "some/view",
body: 42
);
}
}
the framework looks for classes annotated with Route
and wires the dependencies with your code!!!
from zenject-2019.
Ok great :)
Sounds like a cool setup
from zenject-2019.
Related Issues (20)
- User Error -> StackOverflow AssertHit! (999+ times) HOT 2
- Why did you stop publishing in the asset store? HOT 4
- Interfaces and FromComponentInNewPrefab
- Declaring Factory for Object which doesn't have all its dependencies yet
- NullReferenceException in the MonoKernel.Start ()
- Error at project Upgrade HOT 4
- Cant add Zenject-TestFramework to Solution HOT 1
- Injection is happning twice
- Inject reference of a monobehaviour to an instantiated gameobject HOT 1
- "Commitment to open source" and lawsuit HOT 4
- Zenject causes gameObjects destroying just after start
- Typo in readme
- Automocking with Moq internal constructor not visible with Reflection HOT 4
- DiContainer.Resolve exception handling
- Why so many projects and files are added? HOT 1
- ZenjectException: Assert Hit! Expected null pointer but instead found 'Zenject.DiContainer' HOT 1
- DI on base class?
- Objects which class with interface implemented couldn't be inject correctly
- Support for IAsyncDisposable?
- Object that has Reference to the SceneContext stay in memory after scene unloaded HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zenject-2019.