Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Type Class
Type Methods
 GetProperties Method (BindingFlags)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Type..::.GetProperties Method (BindingFlags)

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)
Visual Basic (Declaration)
Public MustOverride Function GetProperties ( _
    bindingAttr As BindingFlags _
) As PropertyInfo()
Visual Basic (Usage)
Dim instance As Type
Dim bindingAttr As BindingFlags
Dim returnValue As PropertyInfo()

returnValue = instance.GetProperties(bindingAttr)
C#
public abstract PropertyInfo[] GetProperties(
    BindingFlags bindingAttr
)
Visual C++
public:
virtual array<PropertyInfo^>^ GetProperties(
    BindingFlags bindingAttr
) abstract
JScript
public abstract function GetProperties(
    bindingAttr : BindingFlags
) : PropertyInfo[]

Parameters

bindingAttr
Type: System.Reflection..::.BindingFlags
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
-or-
Zero, to return nullNothingnullptra null reference (Nothing in Visual Basic).

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

_Type..::.GetProperties(BindingFlags)
IReflect..::.GetProperties(BindingFlags)

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 members) 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 requested property is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns nullNothingnullptra null reference (Nothing in Visual Basic).

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.

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

Visual Basic
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

' 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 TypeMain
    Public Shared Sub Main()
        Dim myType As Type = GetType(MyTypeClass)
        ' Get the public properties.
        Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
        Console.WriteLine("The number of public properties is {0}.", myPropertyInfo.Length.ToString())
        ' Display the public properties.
        DisplayPropertyInfo(myPropertyInfo)
        ' Get the nonpublic properties.
        Dim myPropertyInfo1 As PropertyInfo() = myType.GetProperties((BindingFlags.NonPublic Or BindingFlags.Instance))
        Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length.ToString())
        ' Display the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1)
    End Sub 'Main

    Public Shared Sub DisplayPropertyInfo(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)
            Console.WriteLine("The property name is {0}.", myPropInfo.Name.ToString())
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType.ToString())
        Next i
    End Sub 'DisplayPropertyInfo
End Class 'TypeMain 

C#
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 TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public properties.
        PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
        Console.WriteLine("The mumber of public properties is {0}.", myPropertyInfo.Length);
        // Display the public properties.
        DisplayPropertyInfo(myPropertyInfo);
        // Get the nonpublic properties.
        PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length);
        // Display all the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1);        
    }
    public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo)
    {
        // Display information for all properties.
        for(int i=0;i<myPropertyInfo.Length;i++)
        {
            PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
            Console.WriteLine("The property name is {0}.", myPropInfo.Name);
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
        }
    }
}

Visual C++
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class having three properties.
public ref class MyTypeClass
{
public:

   property String^ MyProperty1 
   {
      String^ get()
      {
         return "hello";
      }
   }

   property String^ MyProperty2 
   {
      String^ get()
      {
         return "hello";
      }
   }

protected:

   property String^ MyProperty3 
   {
      String^ get()
      {
         return "hello";
      }
   }
};

void DisplayPropertyInfo( array<PropertyInfo^>^myPropertyInfo )
{
   // Display information for all properties.
   for ( int i = 0; i < myPropertyInfo->Length; i++ )
   {
      PropertyInfo^ myPropInfo = myPropertyInfo[ i ];
      Console::WriteLine( "The property name is {0}.", myPropInfo->Name );
      Console::WriteLine( "The property type is {0}.", myPropInfo->PropertyType );
   }
}

int main()
{
   Type^ myType = MyTypeClass::typeid;

   // Get the public properties.
   array<PropertyInfo^>^myPropertyInfo = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
   Console::WriteLine( "The mumber of public properties is {0}.", myPropertyInfo->Length );

   // Display the public properties.
   DisplayPropertyInfo( myPropertyInfo );

   // Get the nonpublic properties.
   array<PropertyInfo^>^myPropertyInfo1 = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
   Console::WriteLine( "The number of protected properties is {0}.", myPropertyInfo1->Length );

   // Display all the nonpublic properties.
   DisplayPropertyInfo( myPropertyInfo1 );
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
BindingFlags.FlattenHierarchy does not return inherited interface members when querying an interface      David M. Kean   |   Edit   |   Show History

Passing BindingFlags.FlattenHierarchy to one of the Type.GetXXX methods, such as Type.GetMembers, will not return inherited interface members when you are querying on an interface type itself.

For example, given the following interface types:

    public interface A
{
void AMethod();
}

public interface B
{
void BMethod();
}

public interface AB : A, B
{
void ABMethod();
}

Running the following code, despite passing BindingFlags.FlattenHierarchy, will only return members that are directly declared on the AB interface:

[C#]
using System;
using System.Reflection;

namespace Samples
{
class Program
{
static void Main(string[] args)
{
foreach (MemberInfo member in typeof(AB).GetMembers(BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public))
{
Console.WriteLine(member.Name);
}
}
}
}

The above code outputs the following:

ABMethod

To get the inherited members, you need to query each implemented interface for its members.

The following examples shows a way to do this:

[C#]
using System;
using System.Reflection;
using System.Collections.Generic;

namespace Samples
{
class Program
{
static void Main(string[] args)
{
foreach (MemberInfo member in GetMembers(typeof(AB), BindingFlags.Public | BindingFlags.Instance))
{
Console.WriteLine(member.Name);
}
}

private static ICollection<MemberInfo> GetMembers(Type type, BindingFlags flags)
{
HashSet<MemberInfo> members = new HashSet<MemberInfo>();
GetMembersRecursively(type, flags, members);
return members;
}



private static void GetMembersRecursively(Type type, BindingFlags flags, HashSet<MemberInfo> members)
{
MemberInfo[] childMembers = type.GetMembers(flags);
members.UnionWith(childMembers);

foreach (Type interfaceType in type.GetInterfaces())
{
GetMembersRecursively(interfaceType, flags, members);
}
}
}
}


The above code outputs the following:

ABMethod
AMethod
BMethod
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker