Official Document: Http://docs.autofac.org/en/latest/register/prop-method-injection.html
AUTOFAC properties and Method injection
Although constructor parameter injection is the preferred method for passing values to a component, you can also use a property or method injection to provide a value.
Property injection is injected using a writable property instead of a constructor parameter. Method injection sets the dependency relationship by calling a method.
1. Attribute Injection
If it is a lambda expression component , use an object to initialize it:
New A {B = c.resolve<b> ()});
To support circular dependencies, use an activated event handler:
New A ()). OnActivated (e = e.instance.b = E.context.resolve<b> ());
If it is a reflection component , use propertiesautowired() to modify the registration property:
Builder. Registertype<a> (). Propertiesautowired ();
If you have a specific attribute and value that needs to be connected, you can use withproperty () to modify:
Builder. Registertype<a> (). Withproperty ("PropertyName", PropertyValue);
2. Method Injection
The simplest way to call a method to set the value of a component is to use a lambda expression component and the correct invocation to activate the processing method.
Builder. Register (c + = {varnew myobjecttype () ; var dep = c.resolve<thedependency>(); Result. Setthedependency (DEP); return result;});
If you cannot register with a lambda expression, you can add an activation event handler (activating eventhandler)
Builder . Register<MyObjectType>() = { var dep = e.context.resolve< Thedependency>(); E.instance.setthedependency (DEP); });
autofac--. Registration component--3 properties and method injection