<Property> Element (.NET Native)

Applies runtime reflection policy to a property.

Syntax

<Property Name="property_name"
          Browse="policy_type"
          Dynamic="policy_type"
          Serialize="policy_type" />

Attributes and Elements

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

Attributes

Attribute Attribute type Description
Name General Required attribute. Specifies the property name.
Browse Reflection Optional attribute. Controls querying for information about or enumerating the property but does not enable any dynamic access at run time.
Dynamic Reflection Optional attribute. Controls runtime access to the property to enable dynamic programming. This policy ensures that a property can be set or retrieved dynamically at run time.
Serialize Serialization Optional attribute. Controls runtime access to a property to enable type instances to be serialized by libraries such as the Newtonsoft JSON serializer or to be used for data binding.

Name attribute

Value Description
method_name The property name. The type of the property is defined by the parent <Type> or <TypeInstantiation> element.

All other attributes

Value Description
policy_setting The setting to apply to this policy type for the property. Possible values are Auto, Excluded, Included, and Required. 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.
<TypeInstantiation> Applies reflection policy to a constructed generic type and all its members.

Remarks

If a property's policy is not explicitly defined, it inherits the runtime policy of its parent element.

Example

The following example uses reflection to instantiate a Book object and display its property values. The original default.rd.xml file for the project appears as follows:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
   <Application>
      <Namespace Name="LibraryApplications"  Browse="Required Public" >
         <Type Name="Book"   Activate="All" />
      </Namespace>
   </Application>
</Directives>

The file applies the All value to the Activate policy for the Book class, which allows access to class constructors through reflection. The Browse policy for the Book class is inherited from its parent namespace. This is set to Required Public, which makes metadata available at runtime.

The following is the source code for the example. The outputBlock variable represents a TextBlock control.

namespace LibraryApplications
{
   public sealed class Example
   {
      public void Execute()
      {
         TextBlock outputBlock = MainPage.outputBlock;
         Type t = typeof(Book);
         Object obj = Activator.CreateInstance(t, new Object[] { "A Tale of 2 Cities", "Charles Dickens", "" });
         outputBlock.Text += "\n\n\n";
         foreach (var p in t.GetRuntimeProperties())
         {
            outputBlock.Text += String.Format("{0}: {1}\n", p.Name, p.GetValue(obj));
         }
      }
   }

   public class Book
   {
      private string bookTitle = "";
      private string bookAuthor = "";
      private string bookISBN = "";

      public Book(string title, string author, string isbn)
      {
         bookTitle = title;
         bookAuthor = author;
         bookISBN = isbn;
      }

      public string Title
      {
         get { return bookTitle; }
         set { bookTitle = value; }
      }

      public string Author
      {
         get { return bookAuthor; }
         set { bookTitle = value; }
      }

      public string ISBN
      {
         get { return bookISBN; }
      }

      public override string ToString()
      {
         return String.Format("{0}, {1}", Author, Title);
      }
   }
}

public class TextUtilities
{
   public static string ConvertNumberToWord(int value)
   {
      switch (value)
      {
         case 1:
            return "One";
         case 2:
            return "Two";
         case 3:
            return "Three";
         case 4:
            return "Four";
         case 5:
            return "Five";
         case 6:
            return "Six";
         case 7:
            return "Seven";
         case 8:
            return "Eight";
         case 9:
            return "Nine";
         default:
            return value.ToString();
      }
   }
}

However, compiling and executing this example throws a MissingRuntimeArtifactException exception. Although we've made metadata for the Book type available, we've failed to make the implementations of the property getters available dynamically. We can correct this error by either in one of two ways:

  • by defining the Dynamic policy for the Book type in its <Type> element.

  • By adding a nested <Property> element for each property whose getter we'd like to invoke, as the following default.rd.xml file does.

    <Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
       <Application>
          <Namespace Name="LibraryApplications"  Browse="Required Public" >
             <Type Name="Book"   Activate="All" >
                <Property Name="Title" Dynamic="Required" />
                <Property Name="Author" Dynamic="Required" />
                  <Property Name="ISBN" Dynamic="Required" />
             </Type>
          </Namespace>
       </Application>
    </Directives>
    

See also