|
ResolveParameters
Parameters can be used when registering and resolving components.
Passing Parameters to Resolve()Resolve accepts parameters using a variable-length argument list: var fred = Resolve<Person>(new NamedParameter("name", "Fred"));This will map automatically to the named constructor parameters on the implementation class, if it was registered using Reflection, e.g.: class Person
{
public Person(string name)
...Available Parameter TypesAutofac offers several different parameter matching strategies:
NamedParameter and TypedParameter can supply constant values only. ResolvedParameter can be used as a way to supply values dynamically retrieved from the container, e.g. by resolving a service by name. Utilising Parameters from an ExpressionIf Person is to be registered using an expression, the parameters can be accessed from a second available delegate parameter of type IEnumerable<Parameter>: builder.Register((c, p) => new Person(p.Named<string>("name")));Delegate FactoriesCheck out the delegate factory feature for a cleaner way to create components that require parameters. |
Sign in to add a comment