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

Type.GetProperty Method (String, BindingFlags)

.NET Framework 1.1

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

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

Implements

IReflect.GetProperty

Exceptions

Exception Type Condition
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).

Remarks

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 static properties up the hierarchy.

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

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 in accordance with the specified binding constraints.

[Visual Basic] 

Imports System
Imports System.Reflection
Module Module1
    Public 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
        Public Shared Sub Main()
            Try
                ' Get a Type object corresponding to MyClass.
                Dim myType As Type = GetType(MyClass1)
                ' Get a PropertyInfo object by passing property name and specifying BindingFlags.
                Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty", BindingFlags.Public Or BindingFlags.Instance)
                ' Display the Name property.
                Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name)
            Catch e As NullReferenceException
                Console.WriteLine("MyProperty does not exist in MyClass.", e.Message.ToString())
            End Try
        End Sub 'Main
    End Class 'MyClass1
End Module 'Module1

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

[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 Type object of MyClass.
      Type* myType=__typeof(MyClass);
      // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
      PropertyInfo*  myPropInfo = myType->GetProperty(S"MyProperty", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
      // Display Name propety to console.
      Console::WriteLine(S"{0} is a property of MyClass.", myPropInfo->Name);
   } catch (NullReferenceException* e) {
      Console::WriteLine(S"MyProperty does not exist in MyClass. {0}", e->Message);
   }
}

[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 | BindingFlags | DefaultBinder | ReflectionPermission | GetPropertyImpl | GetProperties

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