The overload with the InjectionMember
array is used, when you do not provide a configuration file that Unity tells how to create an instance of the given type or if you want to create an instance by another way than defined in the configuration file.
The overloads are used, when you want to configure an unity container without a configuration file. An InjectionMember
can be a constructor, property or method call.
The following code, taken from the Unity help, shows how to use InjectionMembers
through the fluent interface of the container.
IUnityContainer myContainer = new UnityContainer();
myContainer.Configure<InjectedMembers>()
.ConfigureInjectionFor<MyObject>(
new InjectionConstructor(12, "Hello Unity!"),
new InjectionProperty("MyStringProperty", "SomeText"));
The following XML configuration is equivalent to the code above.
<type type="MyObject" mapTo="MyObject" name="MyObject">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
<constructor>
<param name="someInt" parameterType="int">
<value value="12"/>
</param>
<param name="someText" parameterType="string">
<value value="Hello Unity!"/>
</param>
</constructor>
<property name="MyStringProperty" propertyType="string">
<value value="SomeText"/>
</property>
</typeConfig>
</type>