<TypeInstantiation> 元素 (.NET Native)

将运行时反射策略应用到一个构造泛型类型。

语法

<TypeInstantiation Name="type_name"
                   Arguments="type_arguments"
                   Activate="policy_type"
                   Browse="policy_type"
                   Dynamic="policy_type"
                   Serialize="policy_type"
                   DataContractSerializer="policy_setting"
                   DataContractJsonSerializer="policy_setting"
                   XmlSerializer="policy_setting"
                   MarshalObject="policy_setting"
                   MarshalDelegate="policy_setting"
                   MarshalStructure="policy_setting" />

特性和元素

下列各节描述了特性、子元素和父元素。

特性

属性 属性类型 说明
Name 常规 必需的特性。 指定类型名称。
Arguments 常规 必需的特性。 指定泛型类型参数。 如果存在多个自变量,它们之间用逗号分割。
Activate 反射 可选特性。 控制运行时对构造函数的访问,以启用实例激活。
Browse 反射 可选特性。 控制对有关程序元素信息的查询,但并不启用任何运行时访问。
Dynamic 反射 可选特性。 控制运行时对所有类型成员的访问,包括构造函数、方法、字段、属性和事件,以启用动态编程。
Serialize 序列化 可选特性。 控制运行时对构造函数、字段和属性的访问,使类型实例得到序列化和反序列化处理,这是通过库进行的,例如 Newtonsoft JSON 序列化程序。
DataContractSerializer 序列化 可选特性。 控制使用 System.Runtime.Serialization.DataContractSerializer 类的序列化策略。
DataContractJsonSerializer 序列化 可选特性。 控制使用 System.Runtime.Serialization.Json.DataContractJsonSerializer 类的 JSON 序列化策略。
XmlSerializer 序列化 可选特性。 控制使用 System.Xml.Serialization.XmlSerializer 类的 XML 序列化策略。
MarshalObject Interop 可选特性。 控制封送引用类型到 Windows 运行时和 COM 的策略。
MarshalDelegate Interop 可选特性。 控制将委托类型作为函数指针封送到本机代码的策略。
MarshalStructure Interop 可选特性。 控制封送结构到本机代码的策略。

Name 特性

Value 说明
type_name 类型名称。 如果该 <TypeInstantiation> 元素是 <Namespace> 元素、<Type> 元素或另一个 <TypeInstantiation> 元素的子元素,type_name 可以指定类型名称而无需其命名空间。 否则,type_name 必须包含完全限定的类型名称。 该类型名称没有经过修饰。 例如,对于一个 System.Collections.Generic.List<T> 对象,<TypeInstantiation> 元素可能显示如下:

\<TypeInstantiation Name=System.Collections.Generic.List Dynamic="Required Public" />

自变量特性

Value 说明
type_argument 指定泛型类型参数。 如果存在多个自变量,它们之间用逗号分割。 每个自变量必须包含一个完全限定的类型名称。

所有其他特性

Value 说明
策略_设置 该设置将应用到这个构造泛型类型的策略类型。 可能值为 AllAutoExcludedPublicPublicAndInternalRequired PublicRequired PublicAndInternal 以及 Required All。 有关详细信息,请参阅运行时指令策略设置

子元素

元素 说明
<事件> 将反射策略应用到属于这种类型的一个事件。
<字段> 将反射策略应用到属于这种类型的一个字段。
<ImpliesType> 如果该策略已应用到以包含 <TypeInstantiation> 元素为代表的类型,将该策略应用到一个类型。
<方法> 将反射策略应用到属于这种类型的一个方法。
<MethodInstantiation> 将反射策略应用到属于这种类型的一个构造泛型方法。
<属性> 将反射策略应用到属于这种类型的一个属性。
<类型> 将反射策略应用到一个嵌套类型。
<TypeInstantiation> 将反射策略应用到一个嵌套的构造泛型类型。

父元素

元素 说明
<应用程序> 作为应用程序范围内的类型和元数据可以反应在运行时间的类型成员的容器而服务。
<Assembly> 将反射策略应用到指定程序集中的所有类型。
<库> 定义包含元数据在运行时间可以用于反射的类型和类型成员的程序集。
<命名空间> 将反射策略应用到命名空间中的所有类型。
<类型> 将反射策略应用到一种类型及其所有成员。
<TypeInstantiation> 将反射策略应用到一种构造泛型类型及其所有成员。

注解

反射、序列化和互操作特性都是可选项。 然而,至少一个特性必须存在。

如果 <TypeInstantiation> 元素是 <Assembly><Namespace><Type> 元素的子元素,它会重写由父元素定义的策略设置。 如果 <Type> 元素定义了一个相应的泛型类型定义,<TypeInstantiation> 元素只会重写指定的构造泛型类型的实例化的运行时反射策略。

示例

以下实例使用反射从一个构造的 Dictionary<TKey,TValue> 对象取回了泛型类型定义。 它还使用反射显示了代表构造泛型类型和构造类型定义的有关 Type 对象的信息。 示例中的 b 变量是 TextBlock 控件。

   public static void GetGenericInfo()
   {
      // Get the type that represents the generic type definition and
      // display information about it.
      Type generic1 = typeof(Dictionary<,>);
      DisplayGenericType(generic1);

      // Get the type that represents a constructed generic type and its
      // generic type definition.
      Dictionary<string, Example> d1 = new Dictionary<string, Example>();
      Type constructed1 = d1.GetType();
      Type generic2 = constructed1.GetGenericTypeDefinition();

      // Display information for the generic type definition, and
      // for the constructed type Dictionary<String, Example>.
      DisplayGenericType(constructed1);
      DisplayGenericType(generic2);

      // Construct an array of type arguments.
      Type[] typeArgs = { typeof(string), typeof(Example) };
      // Construct the type Dictionary<String, Example>.
      Type constructed2 = generic1.MakeGenericType(typeArgs);

      DisplayGenericType(constructed2);

      object o = Activator.CreateInstance(constructed2);

      b.Text += "\r\nCompare types obtained by different methods:\n";
      b.Text += String.Format("   Are the constructed types equal? {0}\n",
                              (d1.GetType() == constructed2));
      b.Text += String.Format("   Are the generic definitions equal? {0}\n",
                              (generic1 == constructed2.GetGenericTypeDefinition()));

      // Demonstrate the DisplayGenericType and
      // DisplayGenericParameter methods with the Test class
      // defined above. This shows base, interface, and special
      // constraints.
      DisplayGenericType(typeof(TestGeneric<>));
   }

   // Display information about a generic type.
   private static void DisplayGenericType(Type t)
   {
      b.Text += String.Format("\n{0}\n", t);
      b.Text += String.Format("   Generic type? {0}\n",
                              t.GetTypeInfo().GenericTypeParameters.Length !=
                              t.GenericTypeArguments.Length);
      b.Text += String.Format("   Generic type definition? {0}\n",
                              ! t.IsConstructedGenericType);

      // Get the generic type parameters.
      Type[] typeParameters = t.GetTypeInfo().GenericTypeParameters;
      if (typeParameters.Length > 0)
      {
         b.Text += String.Format("   {0} type parameters:\n",
                                 typeParameters.Length);
         foreach (Type tParam in typeParameters)
            b.Text += String.Format("      Type parameter: {0} position {1}\n",
                     tParam.Name, tParam.GenericParameterPosition);
      }
      else
      {
         Type[] typeArgs = t.GenericTypeArguments;
         b.Text += String.Format("   {0} type arguments:\n",
                                 typeArgs.Length);
         foreach (var tArg in typeArgs)
               b.Text += String.Format("      Type argument: {0}\n",
                                       tArg);
      }
      b.Text += "\n-------------------------------\n";
   }
}

public interface ITestInterface { }

public class TestBase { }

public class TestGeneric<T> where T : TestBase, ITestInterface, new() { }

public class TestArgument : TestBase, ITestInterface
{
   public TestArgument()
   { }
}

在同 .NET Native 工具链进行编译后,实例在调用 Type.GetGenericTypeDefinition 方法线路中引发了 MissingMetadataException 异常。 你可通过将以下 <TypeInstantiation> 元素添加到运行时指令文件来消除异常并提供必需的元数据:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="*Application*" Dynamic="Required All" />
     <TypeInstantiation Name="System.Collections.Generic.Dictionary"
                        Arguments="System.String,GenericType.Example"
                        Dynamic="Required Public" />
  </Application>
</Directives>

另请参阅