Let's say I have the following hierarchy.
class PersonWithJacket
{
public PersonWithJacket(Jacket jacket)
{
}
}
class Jacket
{
public Jacket(string brand)
{
}
}
I register these classes in my Autofac container
containerBuilder.RegisterType<PersonWithJacket>();
containerBuilder.RegisterType<Jacket>();
At runtime I want to resolve a PersonWithJacket
object and I want to define the string brand
constructor parameter of the nested Jacket
class. My first approach was to use the NamedParameter
like so:
var personWithJacket = container.Resolve<PersonWithJacket>(new NamedParameter("brand", "TheBrand"));
I read that as "resolve the a PersonWithJacket
object and if you encounter a constructor parameter called 'brand', use the value 'TheBrand'" but the string parameter cannot be resolved.
Is there a way to define, at runtime, the value of a constructor parameter of a nested object?