Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

public abstract PropertyInfo[] GetProperties (
	BindingFlags bindingAttr
)
public abstract PropertyInfo[] GetProperties (
	BindingFlags bindingAttr
)
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.

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

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

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.


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);
        }
    }
}

import System.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

// Create a class having three properties.
public class MyTypeClass
{
    /** @property 
     */
    public String get_MyProperty1()
    {
        return "hello";
    }//MyProperty1

    /** @property 
     */
    public String get_MyProperty2()
    {
        return "hello";
    }//MyProperty2

    /** @property 
     */
    protected String get_MyProperty3()
    {
        return "hello";
    }//Myproperty3
} //MyTypeClass

public class TypeMain
{
    public static void main(String[] args)
    {
        Type myType = MyTypeClass.class.ToType();
        // Get the public properties.
        PropertyInfo myPropertyInfo[] = myType.GetProperties(BindingFlags.
            Public | BindingFlags.Instance);
        Console.WriteLine("The mumber of public properties is {0}.",
            System.Convert.ToString(myPropertyInfo.get_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}.",
            System.Convert.ToString(myPropertyInfo1.get_Length()));
        // Display all the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1);
    } //main

    public static void DisplayPropertyInfo(PropertyInfo myPropertyInfo[])
    {
        // Display information for all properties.
        for (int i = 0; i < myPropertyInfo.get_Length(); i++) {
            PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo.get_Item(i);
            Console.WriteLine("The property name is {0}.", myPropInfo.get_Name());
            Console.WriteLine("The property type is {0}.", myPropInfo.GetType());
        }
    } //DisplayPropertyInfo
} //TypeMain

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft