Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Type.GetProperty Method (String)

Searches for the public property with the specified name.

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

public PropertyInfo GetProperty (
	string name
)
public final PropertyInfo GetProperty (
	String name
)
public final function GetProperty (
	name : String
) : PropertyInfo

Parameters

name

The String containing the name of the public property to get.

Return Value

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

AmbiguousMatchException

More than one property is found with the specified name.

ArgumentNullException

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

The search for name is case-sensitive. The search includes public static and public instance properties.

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 object of a user-defined class, retrieves a property of that class, and displays the property name.


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 the Type object corresponding to MyClass.
            Type myType=typeof(MyClass);       
            // Get the PropertyInfo object by passing the property name.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
            // Display the property name.
            Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
        }
        catch(NullReferenceException e)
        {
            Console.WriteLine("The property 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 the Type object corresponding to MyClass.
            Type myType = MyClass .class.ToType();
            // Get the PropertyInfo object by passing the property name.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
            // Display the property name.
            Console.WriteLine("The {0} property exists in MyClass.",
                myPropInfo.get_Name());
        }
        catch (NullReferenceException e) {
            Console.WriteLine("The property does not exist in MyClass." 
                + e.get_Message());
        }
    } //main
} //MyTypeClass

Internally, this property is referred to in the metadata by the name "Item." Any attempt to get PropertyInfo using reflection must specify this internal name in order to correctly return the PropertyInfo property.

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.