Unity Generic Type Support

这次的例子是学习Unity对泛型的支持,先看官方文档的说明吧。

Unity generic parameter injection configuration support for params and properties is provided by the GenericParameter class when using the API and by the genericParameterName for injection by using attributes in configuration files.

Unity always resolves closed generics. There are two ways to configure them:

  • You can configure injection for a specific closed generic type (and only that type) just as you would configure a non-generic type. In this case, Unity handles closed generics exactly as it handles non-generic types, and configuring injection works just as it does for non-generic types by using parameterType, propertyType and ResolvedParameter, InjectionParameter.
  • You can configure injection on the open generic type and it would apply to any closed generic built from it. In this case, when a closed type is resolved, the actual parameters that had as their type one of the open generic type's generic type parameters are used in the GenericParameter and genericParameterName injection process.

Use GenericParameter for run-time support and to configure injection at the open generic type level. Use the genericParameterName configuration element to support use of a .config file.

Setting the genericParameterName causes a GenericParameter to be created, using the value for genericParameterName and, optionally, the dependency element's name as the resolutionKey if such a dependency element exists and has a name.

The genericParameterName element is the name of a generic type parameter in the open generic type for which injection is being configured.

示例:

  1. 先定义一个带有泛型的类,代码如下
     public class GenericTest<T1> : UnityGeneric.IGenericTest<T1>
        {
            private T1 injectedValue;
    
            public T1 InjectedValue
            {
                get { return injectedValue; }
                set { injectedValue = value; }
            }
            public GenericTest(string s, object o)
            {
    
            }
            public GenericTest(T1 injectedValue)
            {
                this.injectedValue = injectedValue;
            }
        }
  2. 然后定义一个注入到GenericTest实例的类。
        public class Users
        {
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
        }
  3. 配置文件,代码如下
    <configuration>
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                 Microsoft.Practices.Unity.Configuration" />
      </configSections>
      <unity>
        <typeAliases>
          <typeAlias alias="IGenericTest" type="UnityGeneric.IGenericTest`1,UnityGeneric"/>
          <typeAlias alias="GenericTest" type="UnityGeneric.GenericTest`1,UnityGeneric"/>
          <typeAlias alias="Users" type="UnityGeneric.Users,UnityGeneric"/>
          <typeAlias alias="String" type="System.String,mscorlib"/>
        </typeAliases>
        <containers>
    
          <container>
            <types>
    
              <type name="user" type="Users">
                <typeConfig>
                  <property name="Name" propertyType="String">
                    <value value="测试"/>
                  </property>
                </typeConfig>
              </type>
              <type type="IGenericTest" mapTo="GenericTest">
                <typeConfig>
                  <constructor>
                    <param name="injectValue" genericParameterName="T1">
                      <dependency name="user" />
                    </param>
                  </constructor>
                </typeConfig>
              </type>
            </types>
          </container>
        </containers>
      </unity>
    </configuration>
  4. 在容器中获取
    Program

posted @ 2010-02-08 10:19  StreamFei  阅读(719)  评论(0编辑  收藏  举报