Array supported during runtime configuration
Class Foot {
Public Foot (IBar [] bars)
{}
}
Container. Configure <InjectedMembers> (). ConfigureInjectionFor <Foot> (new InjectionConstructor (new ResolvedArrayParameter <IBar> ());
Container. configure <InjectedMembers> (). configureInjectionFor <Foot> (new InjectionConstructor (new ResolvedArrayParameter <IBar> (typeof (IBar), new ResolvedParameter <IBar> ("some named bar"), new BarImpl ()))
Configure containers during running
1. Configure constructors, attributes, and method injection.
You can directly create and load configurations during running to configure the Unity container to implement the injection function. In addition to adding attributes and using configuration files, you can also use this method.
Use the InjectionConstructor, InjectionProperty, and InjectionMethod classes to combine the Configure and ConfigureInjectionFor methods to specify the dependency injection parameters for the container.
IUnityContainer container = new UnityContainer (). RegisterType <AType> (new partition (); AType aType = container. Resolve <AType> (); Assert. IsTrue (aType. DefaultConstructorCalled );
The following code uses the Configuration class to configure constructor injection, property injection, and method injection.
IUnityContainer myContainer = new UnityContainer (); myContainer. Configure <InjectedMembers> (). ConfigureInjectionFor <MyObject> (new InjectionConstructor (12, "Hello Unity! "), New InjectionProperty (" MyProperty "), new InjectionProperty (" MyStringProperty "," SomeText "), new InjectionMethod (" InitializeMe ", 42.0, new ResolvedParameter (typeof (ILogger ), "SpecialLogger ")));
This kind of API dynamic configuration injection provides sufficient flexibility and retains a simple and intuitive way of use.
This configuration injection API is based on the subclass of InjectionParameterValue. Of course, other types of objects can also be provided when injection is created. According to the following rules, the provided object will be translated into the InjectionParameterValue type.
1) if the object is a subclass instance of InjectionParameterValue, the injection system will use this object.
2) if the object is a Type instance, the injection system will create a ResolvedParameter as the Type returned after the injection.
3) If it is another object, the configuration API will create an InjectionParameterValue instance, and the container will generate an attribute injection.