<Subtypes> Element (.NET Native)

Applies runtime policy to all classes inherited from the containing type.

Syntax

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

All 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

None.

Parent Elements

Element Description
<Type> Applies reflection policy to a type and all its members.

Remarks

The <Subtypes> element applies policy to all the subtypes of its containing type. You use it when you want to apply different policies to derived types and their base classes.

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

Example

The following example defines a class named BaseClass and a subclass named Derived1.

namespace Examples.Libraries
{
   public class BaseClass
   {
      public BaseClass()
      { }

      public override string ToString()
      {
          return String.Format("{0} Version  {1}", this.GetType().Name, Version);
      }
      public virtual double Version
      { get { return 1.0; }}
    }

   public class Derived1 : BaseClass
   {
      public Derived1() : base()
      {}

      public override double Version
      { get { return 1.1; }}

      public override string ToString()
      {
         return String.Format("{0} Version {1}", this.GetType().Name, Version);
      }
   }
}

As shown in the following code, the runtime directives file explicitly sets the Dynamic and Activate policies for BaseClass to Excluded. Because of this, objects of type BaseClass cannot be instantiated dynamically or by calls to the BaseClass class constructor. However, the <Subtypes> element allows classes derived from BaseClass to be instantiated dynamically and through calls to their class constructors.

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
   <Assembly Name="*Application*" Dynamic="Required All" />
     <Type Name="Examples.Libraries.BaseClass" Activate ="Excluded" Dynamic="Excluded" >
        <Subtypes Activate="Public" Dynamic ="Public"/>
     </Type>
  </Application>
</Directives>

Because of the <Subtypes> directive, the following code that instantiates a Derived1 instance dynamically by calling the Activator.CreateInstance(Type) method executes successfully. The block variable here is a TextBlock object in a blank UWP app.

namespace Examples
{
   using System.Reflection;
   using Examples.Libraries;

   public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            Derived1 d1 = new Derived1();
            block.Text += d1.ToString() + Environment.NewLine;

            Type derivedType = typeof(Derived1);
            Object obj = Activator.CreateInstance(derivedType);
            block.Text += obj.GetType().FullName + Environment.NewLine;
        }
    }
}

See also