Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.GetProperties Method (BindingFlags)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public MustOverride Function GetProperties( _
   ByVal bindingAttr As BindingFlags _
) As PropertyInfo() Implements IReflect.GetProperties
[C#]
public abstract PropertyInfo[] GetProperties(
 BindingFlags bindingAttr
);
[C++]
public: virtual PropertyInfo* GetProperties(
 BindingFlags bindingAttr
) [] = 0;
[JScript]
public abstract function GetProperties(
   bindingAttr : BindingFlags
) : PropertyInfo[];

Parameters

bindingAttr
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

Return Value

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

Remarks

A property is considered public to reflection if it has at least one accessor that is public. That is, you can call type.GetProperty("propertyname", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) to get it.

Otherwise, the property is private and you must use type.GetProperty("propertyname", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) to get it.

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 and protected members) in the search.
  • Specify BindingFlags.FlattenHierarchy to include static properties up the hierarchy.

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.

If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] 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);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class having three properties.
public __gc class MyTypeClass {
public:
   __property String* get_MyProperty1() {
      return S"hello";
   }
   __property String* get_MyProperty2() {
      return S"hello";
   }
protected:
   __property String* get_MyProperty3() {
      return S"hello";
   }
};

void DisplayPropertyInfo(PropertyInfo* myPropertyInfo[]) {
   // Display information for all properties.
   for (int i=0;i<myPropertyInfo->Length;i++) {
      PropertyInfo*  myPropInfo = myPropertyInfo[i];
      Console::WriteLine(S"The property name is {0}.", myPropInfo->Name);
      Console::WriteLine(S"The property type is {0}.", myPropInfo->PropertyType);
   }
}
int main() {
   Type* myType =(__typeof(MyTypeClass));
   // Get the public properties.
   PropertyInfo*  myPropertyInfo[] = myType->GetProperties(static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance));
   Console::WriteLine(S"The mumber of public properties is {0}.",__box(  myPropertyInfo->Length));
   // Display the public properties.
   DisplayPropertyInfo(myPropertyInfo);
   // Get the nonpublic properties.
   PropertyInfo*  myPropertyInfo1[] = myType->GetProperties(static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance));
   Console::WriteLine(S"The number of protected properties is {0}.",__box( myPropertyInfo1->Length));
   // Display all the nonpublic properties.
   DisplayPropertyInfo(myPropertyInfo1);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | Type.GetProperties Overload List | PropertyInfo | BindingFlags | DefaultBinder | GetProperty

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.