Export (0) Print
Expand All

Type.GetProperty Method (String)

.NET Framework 1.1

Searches for the public property with the specified name.

[Visual Basic]
Overloads Public Function GetProperty( _
   ByVal name As String _
) As PropertyInfo
[C#]
public PropertyInfo GetProperty(
 string name
);
[C++]
public: PropertyInfo* GetProperty(
 String* name
);
[JScript]
public 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).

Exceptions

Exception Type Condition
AmbiguousMatchException More than one property is found with the specified name.
ArgumentNullException name is a null reference (Nothing in Visual Basic).

Remarks

The search for name is case-sensitive.

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

[Visual Basic] 
Imports System
Imports System.Reflection
Class MyClass1
    Private myProperty1 As Integer
    ' Declare MyProperty.

    Public Property MyProperty() As Integer
        Get
            Return myProperty1
        End Get
        Set(ByVal Value As Integer)
            myProperty1 = Value
        End Set
    End Property
End Class 'MyClass1

Public Class MyTypeClass
    Public Shared Sub Main(ByVal args() As String)
        Try
            ' Get Type Object corresponding to MyClass.
            Dim myType As Type = GetType(MyClass1)
            ' Get PropertyInfo object by passing property name.
            Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty")
            ' Display Name propety to console.
            Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name)
        Catch e As NullReferenceException
            Console.WriteLine("The property does not exist in MyClass.", e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'MyTypeClass 

[C#] 

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

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

__gc class MyClass {
private:
   int  myProperty;
   // Declare MyProperty.
public:
   __property int get_MyProperty() {
      return myProperty;
   }
   __property void set_MyProperty(int value) {
      myProperty=value;
   }
};

int main() {
   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(S"MyProperty");
      // Display the property name.
      Console::WriteLine(S"The {0} property exists in MyClass.", myPropInfo->Name);
   } catch (NullReferenceException* e) {
      Console::WriteLine(S"The property does not exist in MyClass. {0}", e->Message);
   }
}

[Visual Basic, C#, C++] Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to get PropertyInfo using reflection must specify this internal name in order for the PropertyInfo to be returned correctly.

[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.GetProperty Overload List | PropertyInfo | String | DefaultBinder | ReflectionPermission | GetPropertyImpl | GetProperties

Show:
© 2014 Microsoft