Type.GetProperty Method (String, BindingFlags)
Assembly: mscorlib (in mscorlib.dll)
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).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 namespace System; using namespace System::Reflection; ref class MyClass { private: int myProperty; public: property int MyProperty { // Declare MyProperty. int get() { return myProperty; } void set( int value ) { myProperty = value; } } }; int main() { try { // Get Type object of MyClass. Type^ myType = MyClass::typeid; // Get the PropertyInfo by passing the property name and specifying the BindingFlags. PropertyInfo^ myPropInfo = myType->GetProperty( "MyProperty", static_cast<BindingFlags>(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. {0}", 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.