Upravit

Sdílet prostřednictvím


<Type> Element (.NET Native)

Applies runtime policy to a particular type, such as a class or structure.

Syntax

<Type Name="type_name"
      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.
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 value types to native code.

Name attribute

Value Description
type_name The type name. If this <Type> element is the child of either a <Namespace> element or another <Type> element, type_name can include the name of the type without its namespace. Otherwise, type_name must include the fully qualified type name.

All other attributes

Value Description
policy_setting The setting to apply to this policy 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
<AttributeImplies> If the containing type is an attribute, defines runtime policy for code elements to which the attribute is applied.
<Event> Applies reflection policy to an event belonging to this type.
<Field> Applies reflection policy to a field belonging to this type.
<GenericParameter> Applies policy to the parameter type of a generic type.
<ImpliesType> Applies policy to a type, if that policy has been applied to the type represented by the containing <Type> 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.
<Subtypes> Applies runtime policy to all classes inherited from the containing type.
<Type> Applies reflection policy to a nested type.
<TypeInstantiation> Applies reflection policy to a 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. If none are present, the <Type> element serves as a container whose child types define policy for individual members.

If a <Type> element is the child of an <Assembly>, <Namespace>, <Type>, or <TypeInstantiation> element, it overrides the policy settings defined by the parent element.

A <Type> element of a generic type applies its policy to all instantiations that do not have their own policy. The policy of constructed generic types is defined by the <TypeInstantiation> element.

If the type is a generic type, its name is decorated by a grave accent symbol (`) followed by its number of generic parameters. For example, the Name attribute of a <Type> element for the System.Collections.Generic.List<T> class appears as Name="System.Collections.Generic.List`1".

Example 1

The following example uses reflection to display information about the fields, properties, and methods of the System.Collections.Generic.List<T> class. The variable b in the example is a TextBlock control. Because the example simply retrieves type information, the availability of metadata is controlled by the Browse policy setting.

public static void GetReflectionInfo()
{
   Type t = typeof(List<>);
   b.Text += String.Format("Type information for {0}\n", t);

   // Get fields.
   b.Text += "\nFields:\n";

   var fields = t.GetTypeInfo().DeclaredFields;
   int nFields = 0;
   foreach (var field in fields)
   {
      b.Text += String.Format("   {0} ({1})", field.Name, field.FieldType.Name);
      nFields++;
   }
   if (nFields == 0) b.Text += "   None\n";

   // Get properties.
   b.Text += "\nProperties:\n";
   var props = t.GetTypeInfo().DeclaredProperties;
   int nProps = 0;
   foreach (var prop in props)
   {
      b.Text += String.Format("   {0} ({1})\n", prop.Name, prop.PropertyType.Name);
      nProps++;
   }
   if (nProps == 0) b.Text += "   None\n";

   // Get methods.
   b.Text += "\nMethods:\n";
   var methods = t.GetTypeInfo().DeclaredMethods;
   int nMethods = 0;
   foreach (var method in methods)
   {
      if (method.IsSpecialName) continue;
      b.Text += String.Format("   {0}({1}) ({2})\n", method.Name,
                              GetSignature(method), method.ReturnType.Name);
      nMethods++;
   }
   if (nMethods == 0) b.Text += "   None\n";
}

private static string GetSignature(MethodInfo m)
{
   string signature = null;
   var parameters = m.GetParameters();
   for (int ctr = 0; ctr < parameters.Length; ctr++)
   {
      signature += String.Format("{0} {1}", parameters[ctr].ParameterType.Name,
                                  parameters[ctr].Name);
      if (ctr < parameters.Length - 1) signature += ", ";
   }
   return signature;
}

Because metadata for the List<T> class isn't automatically included by the .NET Native tool chain, the example fails to display the requested member information at run time. To provide the necessary metadata, add the following <Type> element to the runtime directives file. Note that, because we've provided a parent <Namespace> element, we don't have to provide a fully qualified type name in the <Type> element.

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
   <Application>
      <Assembly Name="*Application*" Dynamic="Required All" />
      <Namespace Name ="System.Collections.Generic" >
        <Type Name="List`1" Browse="Required All" />
     </Namespace>
   </Application>
</Directives>

Example 2

The following example uses reflection to retrieve a PropertyInfo object that represents the String.Chars[] property. It then uses the PropertyInfo.GetValue(Object, Object[]) method to retrieve the value of the seventh character in a string, and to display all the characters in the string. The variable b in the example is a TextBlock control.

public void Example()
{
  string test = "abcdefghijklmnopqrstuvwxyz";

  // Get a PropertyInfo object.
  TypeInfo ti = typeof(string).GetTypeInfo();
  PropertyInfo pinfo = ti.GetDeclaredProperty("Chars");

  // Show the seventh letter ('g')
  object[] indexArgs = { 6 };
  object value = pinfo.GetValue(test, indexArgs);
  b.Text += String.Format("Character at position {0}: {1}\n", indexArgs[0], value);

  // Show the complete string.
  b.Text += "\nThe complete string:\n";
  for (int x = 0; x < test.Length; x++)
  {
      b.Text += pinfo.GetValue(test, new Object[] {x}).ToString() + " ";
  }
}
// The example displays the following output:
//       Character at position 6: g
//
//       The complete string:
//       a b c d e f g h i j k l m n o p q r s t u v w x y z

Because metadata for the String object isn't available, the call to the PropertyInfo.GetValue(Object, Object[]) method throws a NullReferenceException exception at run time when compiled with the .NET Native tool chain. To eliminate the exception and provide the necessary metadata, add the following <Type> element to the runtime directives file:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="*Application*" Dynamic="Required All" />
    <Type Name="System.String" Dynamic="Required Public"/> -->
  </Application>
</Directives>

See also