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

Type.GetProperty Method (String, BindingFlags)

Searches for the specified property, using the specified binding constraints.

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

public PropertyInfo GetProperty (
	string name,
	BindingFlags bindingAttr
)
public final PropertyInfo GetProperty (
	String name, 
	BindingFlags bindingAttr
)
public final function GetProperty (
	name : String, 
	bindingAttr : BindingFlags
) : PropertyInfo

Parameters

name

The String containing the name of the property to get.

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

A PropertyInfo object representing the property that matches the specified requirements, if found; otherwise, a null reference (Nothing in Visual Basic).
Exception typeCondition

AmbiguousMatchException

More than one property is found with the specified name and matching the specified binding constraints.

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

The types array and the modifiers array have the same length. A parameter specified in the types array can have the following attributes, which are specified in the modifiers array: pdIn, pdOut, pdLcid, pdRetval, pdOptional, and pdHasDefault, which represent [In], [Out], [lcid], [retval], [optional], and a value specifying whether the parameter has a default value. A parameter's associated attributes are stored in the metadata and enhance interoperability.

The following BindingFlags filter flags can be used to define which properties 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 properties) 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.IgnoreCase to ignore the case of name.

  • 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 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 retrieves the type of a user-defined class, retrieves a property of that class and displays the property name in accordance with the specified binding constraints.


using System;
using System.Reflection;
class MyClass
{
    private int myProperty;
    // Declare MyProperty.
    public int MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty=value;
        }
    }
}
public class MyTypeClass
{
    public static void Main(string[] args)
    {
        try
        {
            // Get Type object of MyClass.
            Type myType=typeof(MyClass);       
            // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty", BindingFlags.Public | BindingFlags.Instance);
            // Display Name propety to console.
            Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name);
        }
        catch(NullReferenceException e)
        {
            Console.WriteLine("MyProperty does not exist in MyClass." +e.Message);
        }
    }
}

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

class MyClass
{
    private int myProperty;

    // Declare MyProperty.
    /** @property */
    public int get_MyProperty()
    {
        return myProperty;
    } //MyProperty

    /** @property */
    public void set_MyProperty(int value)
    {
        myProperty = value;
    } //MyProperty
} //MyClass

public class MyTypeClass
{
    public static void main(String[] args)
    {
        try {
            // Get Type object of MyClass.
            Type myType = MyClass.class.ToType();
            // Get the PropertyInfo by passing the property name and
            // specifying the BindingFlags.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty",
                BindingFlags.Public | BindingFlags.Instance);
            // Display Name propety to console.
            Console.WriteLine("{0} is a property of MyClass.",
                myPropInfo.get_Name());
        }
        catch (NullReferenceException e) {
            Console.WriteLine("MyProperty does not exist in MyClass." 
                + e.get_Message());
        }
    } //main
} //MyTypeClass

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.