Compartir a través de


Type.GetProperties Method (BindingFlags)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When overridden in a derived class, searches for the properties of the current Type, using the specified binding constraints.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public MustOverride Function GetProperties ( _
    bindingAttr As BindingFlags _
) As PropertyInfo()
public abstract PropertyInfo[] GetProperties(
    BindingFlags bindingAttr
)

Parameters

Return Value

Type: array<System.Reflection.PropertyInfo[]
An array of PropertyInfo objects representing all properties of the current Type that match the specified binding constraints.
-or-
An empty array of type PropertyInfo, if the current Type does not have properties, or if none of the properties match the binding constraints.

Implements

IReflect.GetProperties(BindingFlags)

Remarks

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.

The following BindingFlags filter flags can be used to define which nested types to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • Specify BindingFlags.Public to include public properties in the search.

  • Specify BindingFlags.NonPublic to include non-public properties (that is, private, internal, and protected properties) in the search. Only protected and internal properties on base classes are returned; private properties on base classes are not returned.

  • Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited.

See System.Reflection.BindingFlags for more information.

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

If the current T:System.Type represents a constructed generic type, this method returns the PropertyInfo objects with the type parameters replaced by the appropriate type arguments.

If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

Examples

The following example creates two public properties and one protected property and displays information for the properties that match the specified binding constraints.

Imports System.Reflection
Imports System.Reflection.Emit

' Create a class having three properties.
Public Class MyTypeClass
   Public ReadOnly Property MyProperty1() As [String]
      Get
         Return "hello"
      End Get
   End Property
   Public ReadOnly Property MyProperty2() As [String]
      Get
         Return "hello"
      End Get
   End Property
   Protected ReadOnly Property MyProperty3() As [String]
      Get
         Return "hello"
      End Get
   End Property
End Class 'MyTypeClass

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myType As Type = GetType(MyTypeClass)
      ' Get the public properties.
      Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
      outputBlock.Text += String.Format("The number of public properties is {0}.", myPropertyInfo.Length.ToString()) & vbCrLf
      ' Display the public properties.
      DisplayPropertyInfo(outputBlock, myPropertyInfo)
      ' Get the nonpublic properties.
      Dim myPropertyInfo1 As PropertyInfo() = myType.GetProperties((BindingFlags.NonPublic Or BindingFlags.Instance))
      outputBlock.Text += String.Format("The number of protected properties is {0}.", myPropertyInfo1.Length.ToString()) & vbCrLf
      ' Display the nonpublic properties.
      DisplayPropertyInfo(outputBlock, myPropertyInfo1)
   End Sub 'Main

   Public Shared Sub DisplayPropertyInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myPropertyInfo() As PropertyInfo)
      ' Display the information for all properties.
      Dim i As Integer
      For i = 0 To myPropertyInfo.Length - 1
         Dim myPropInfo As PropertyInfo = CType(myPropertyInfo(i), PropertyInfo)
         outputBlock.Text += String.Format("The property name is {0}.", myPropInfo.Name.ToString()) & vbCrLf
         outputBlock.Text += String.Format("The property type is {0}.", myPropInfo.PropertyType.ToString()) & vbCrLf
      Next i
   End Sub 'DisplayPropertyInfo
End Class 'TypeMain 

using System;
using System.Reflection;
using System.Reflection.Emit;

// Create a class having three properties.
public class MyTypeClass
{
   public String MyProperty1
   {
      get
      {
         return "hello";
      }
   }
   public String MyProperty2
   {
      get
      {
         return "hello";
      }
   }
   protected String MyProperty3
   {
      get
      {
         return "hello";
      }
   }
}

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Type myType = (typeof(MyTypeClass));
      // Get the public properties.
      PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
      outputBlock.Text += String.Format("The mumber of public properties is {0}.", myPropertyInfo.Length) + "\n";
      // Display the public properties.
      DisplayPropertyInfo(outputBlock, myPropertyInfo);
      // Get the nonpublic properties.
      PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);
      outputBlock.Text += String.Format("The number of protected properties is {0}.", myPropertyInfo1.Length) + "\n";
      // Display all the nonpublic properties.
      DisplayPropertyInfo(outputBlock, myPropertyInfo1);
   }
   public static void DisplayPropertyInfo(System.Windows.Controls.TextBlock outputBlock, PropertyInfo[] myPropertyInfo)
   {
      // Display information for all properties.
      for (int i = 0; i < myPropertyInfo.Length; i++)
      {
         PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
         outputBlock.Text += String.Format("The property name is {0}.", myPropInfo.Name) + "\n";
         outputBlock.Text += String.Format("The property type is {0}.", myPropInfo.PropertyType) + "\n";
      }
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.