In Autofac
is it possible to make TypedParameter
lazy? Even more, I need access to container when injecting parameter. Code could look like this:
builder.RegisterType<RootService>()
.WithParameter(TypedParameter.From(c => c.Resolve<IChildService>(key)));
Based on Nick's answer I've created the following helper method:
public static class TypedResolvedParameter
{
public static ResolvedParameter From<T>(Func<IComponentContext, T> factory)
{
return new ResolvedParameter(
(pi, c) => pi.ParameterType == typeof(T),
(pi, c) => factory(c));
}
}
You're looking for ResolvedParameter
, also available as an overload to WithParameter()
:
builder.RegisterType<RootService>()
.WithParameter((pi, c) => pi.ParameterType == typeof(IChildService),
(pi, c) => c.ResolveNamed<IChildService>(key));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With