<TypeInstantiation> 要素 (.NET ネイティブ)

構築されたジェネリック型にランタイム リフレクション ポリシーを適用します。

構文

<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 属性

[値] 説明
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" />

引数属性

[値] 説明
type_argument ジェネリック型引数を指定します。 複数の引数が存在する場合は、コンマで区切られます。 各引数は、完全修飾型名で構成されている必要があります。

その他すべての属性

[値] 説明
policy_setting 構築されたジェネリック型のこのポリシーの種類に適用する設定です。 指定できる値は、AllAutoExcludedPublicPublicAndInternalRequired PublicRequired PublicAndInternal、および Required All です。 詳細については、「ランタイム ディレクティブのポリシー設定」を参照してください。

子要素

要素 説明
<イベント> この型に属するイベントにリフレクション ポリシーを適用します。
<フィールド> この型に属するフィールドにリフレクション ポリシーを適用します。
<ImpliesType> 型にポリシーを適用します (それを含む <TypeInstantiation> 要素により表される型にそのポリシーが適用されている場合)。
<メソッド> この型に属するメソッドにリフレクション ポリシーを適用します。
<MethodInstantiation> この型に属する構築されたジェネリック メソッドにリフレクション ポリシーを適用します。
<プロパティ> この型に属するプロパティにリフレクション ポリシーを適用します。
<種類> 入れ子になった型にリフレクション ポリシーを適用します。
<TypeInstantiation> 入れ子になった構築されたジェネリック型にリフレクション ポリシーを適用します。

親要素

要素 説明
<Application> 実行時にリフレクションで使用可能なメタデータを持つ、アプリケーション全体の型と型のメンバーのコンテナーとして機能します。
<アセンブリ> 指定したアセンブリ内のすべての型にリフレクション ポリシーを適用します。
<ライブラリ> 実行時にリフレクションに使用可能なメタデータを持つ型と型のメンバーを含むアセンブリを定義します。
<名前空間> 名前空間内のすべての型にリフレクション ポリシーを適用します。
<種類> 型とそのすべてのメンバーにリフレクション ポリシーを適用します。
<TypeInstantiation> 構築されたジェネリック型とそのすべてのメンバーにリフレクション ポリシーを適用します。

解説

リフレクション、シリアル化、および相互運用属性はすべて省略可能です。 ただし、そのうち少なくとも 1 つが存在する必要があります。

<TypeInstantiation> 要素が <Assembly><Namespace>、または <Type> 要素の子である場合、親要素により定義されたポリシー設定をオーバーライドします。 <Type> 要素が対応するジェネリック型定義を定義している場合、<TypeInstantiation> 要素は、指定の構築されたジェネリック型のインスタンス化についてのみランタイム リフレクション ポリシーをオーバーライドします。

次の例では、リフレクションを使用して、構築された Dictionary<TKey,TValue> オブジェクトからジェネリック型定義を取得します。 また、リフレクションを使用して、構築されたジェネリック型とジェネリック型定義を表す Type オブジェクトに関する情報も表示します。 例の変数 bTextBlock コントロールです。

   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 ネイティブ ツール チェーンでコンパイルされると、この例は 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>

関連項目