I am registering a type in MVVMLight SimpleIoc,
SimpleIoc.Default.Register<MyInjectingClass>();
Then I do a constructor injection of this type as a Func
,
public class MyConsumerClass
{
readonly Func<MyInjectingClass> _injectingClassFactory;
public MyConsumerClass(Func<MyInjectingClass> injectingClassFactory)
{
_injectingClassFactory = injectingClassFactory;
}
}
But at runtime, I get this error,
Type not found in cache: System.Func`1[[<...>.MyInjectingClass, <...>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
How can I constructor inject a type as a Func
?
Note:
I'm doing this stuff in a Xamarin.IOs project. The NuGet I use for MVVMLight SimpleIoc is this.
I figured out that, to inject a type as a Func
, then you have to
Func
(consuming the above registered type)That is,
SimpleIoc.Default.Register<MyInjectingClass>(); // should happen first
SimpleIoc.Default.Register<Func<MyInjectingClass>>(
() => () => SimpleIoc.Default.GetInstance<MyInjectingClass>(
Guid.NewGuid().ToString()
));
The type is simply registered in a way telling it to return a Func
object of the MyInjectingClass, that retrieves a new object each time.
Guid.NewGuid().ToString()
makes sure a new MyInjectingClass object is returned for the SimpleIoc.Default.GetInstance<MyInjectingClass>
method.