<TypeInstantiation> Element (.NET Native)

Applies runtime reflection policy to a constructed generic type.

Syntax

<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" />  

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute Attribute type Description
Name General Required attribute. Specifies the type name.
Arguments General Required attribute. Specifies the generic type arguments. If multiple arguments are present, they are separated by commas.
Activate Reflection Optional attribute. Controls runtime access to constructors to enable activation of instances.
Browse Reflection Optional attribute. Controls querying for information about program elements, but does not enable any runtime access.
Dynamic Reflection Optional attribute. Controls runtime access to all type members, including constructors, methods, fields, properties, and events, to enable dynamic programming.
Serialize Serialization Optional attribute. Controls runtime access to constructors, fields, and properties, to enable type instances to be serialized and deserialized by libraries such as the Newtonsoft JSON serializer.
DataContractSerializer Serialization Optional attribute. Controls policy for serialization that uses the System.Runtime.Serialization.DataContractSerializer class.
DataContractJsonSerializer Serialization Optional attribute. Controls policy for JSON serialization that uses the System.Runtime.Serialization.Json.DataContractJsonSerializer class.
XmlSerializer Serialization Optional attribute. Controls policy for XML serialization that uses the System.Xml.Serialization.XmlSerializer class.
MarshalObject Interop Optional attribute. Controls policy for marshaling reference types to Windows Runtime and COM.
MarshalDelegate Interop Optional attribute. Controls policy for marshaling delegate types as function pointers to native code.
MarshalStructure Interop Optional attribute. Controls policy for marshaling structures to native code.

Name attribute

Value Description
type_name The type name. If this <TypeInstantiation> element is the child of a <Namespace> element, a <Type> element, or another <TypeInstantiation> element, type_name can specify the name of the type without its namespace. Otherwise, type_name must include the fully qualified type name. The type name isn't decorated. For example, for a System.Collections.Generic.List<T> object, the <TypeInstantiation> element might appear as follows:

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

Arguments attribute

Value Description
type_argument Specifies the generic type arguments. If multiple arguments are present, they are separated by commas. Each argument must consist of the fully qualified type name.

All other attributes

Value Description
policy_setting The setting to apply to this policy type for the constructed generic type. Possible values are All, Auto, Excluded, Public, PublicAndInternal, Required Public, Required PublicAndInternal, and Required All. For more information, see Runtime Directive Policy Settings.

Child Elements

Element Description
<Event> Applies reflection policy to an event belonging to this type.
<Field> Applies reflection policy to a field belonging to this type.
<ImpliesType> Applies policy to a type, if that policy has been applied to the type represented by the containing <TypeInstantiation> element.
<Method> Applies reflection policy to a method belonging to this type.
<MethodInstantiation> Applies reflection policy to a constructed generic method belonging to this type.
<Property> Applies reflection policy to a property belonging to this type.
<Type> Applies reflection policy to a nested type.
<TypeInstantiation> Applies reflection policy to a nested constructed generic type.

Parent Elements

Element Description
<Application> Serves as a container for application-wide types and type members whose metadata is available for reflection at run time.
<Assembly> Applies reflection policy to all the types in a specified assembly.
<Library> Defines the assembly that contains types and type members whose metadata is available for reflection at run time.
<Namespace> Applies reflection policy to all the types in a namespace.
<Type> Applies reflection policy to a type and all its members.
<TypeInstantiation> Applies reflection policy to a constructed generic type and all its members.

Remarks

The reflection, serialization, and interop attributes are all optional. However, at least one must be present.

If a <TypeInstantiation> element is the child of an <Assembly>, <Namespace>, or <Type>, element, it overrides the policy settings defined by the parent element. If a <Type> element defines a corresponding generic type definition, the <TypeInstantiation> element overrides runtime reflection policy only for instantiations of the specified constructed generic type.

Example

The following example uses reflection to retrieve the generic type definition from a constructed Dictionary<TKey,TValue> object. It also uses reflection to display information about Type objects that represent constructed generic types and generic type definitions. The variable b in the example is a TextBlock control.

   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()
   { }
}

After compilation with the .NET Native tool chain, the example throws a MissingMetadataException exception on the line that calls the Type.GetGenericTypeDefinition method. You can eliminate the exception and provide the necessary metadata by adding the following <TypeInstantiation> element to the runtime directives file:

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

See Also

Runtime Directives (rd.xml) Configuration File Reference
Runtime Directive Elements
Runtime Directive Policy Settings