Extenject icon indicating copy to clipboard operation
Extenject copied to clipboard

Instantiating with null extra args ends with exception

Open martin-obert opened this issue 3 years ago • 1 comments

Once creating game object for component via IInstantiator, like:

_instantiator.InstantiateComponent<MyComponent>(_executor, new object[] { task = task, onSuccess = onSuccess // <-- this is null });

ends with error indebug:

ZenjectException: Assert hit! Cannot include null values when creating a zenject argument list because zenject has no way of deducing the type from a null value. If you want to allow null, use the Explicit form.

what does the If you want to allow null, use the Explicit form actually means? Cannot find any official docs on it.

martin-obert avatar Aug 13 '22 21:08 martin-obert

you need to pass a List<TypeValuePair> manually, since Zenject can't determine the contract type of null (using GetType()). So you need to use the _instantiator.InstantiateComponentExplicit<MyComponent>() method instead. And instead of using new object[] { ... } you need to use the more verbose strategy var args = new List<TypeValuePair>() { new TypeValuePair(typeof(TypeOfYourNullThing), null) } ;

Monsoonexe avatar Aug 27 '24 17:08 Monsoonexe