Edit

Share via


Type.GetProperty Method

Definition

Gets a specific property of the current Type.

Overloads

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

Searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints.

GetProperty(String, Type, Type[], ParameterModifier[])

Searches for the specified public property whose parameters match the specified argument types and modifiers.

GetProperty(String, Type[])

Searches for the specified public property whose parameters match the specified argument types.

GetProperty(String, Type, Type[])

Searches for the specified public property whose parameters match the specified argument types.

GetProperty(String, BindingFlags)

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

GetProperty(String)

Searches for the public property with the specified name.

GetProperty(String, Type)

Searches for the public property with the specified name and return type.

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints.

C#
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
C#
public System.Reflection.PropertyInfo GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);

Parameters

name
String

The string containing the name of the property to get.

bindingAttr
BindingFlags

A bitwise combination of the enumeration values that specify how the search is conducted.

-or-

Default to return null.

binder
Binder

An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.

-or-

A null reference (Nothing in Visual Basic), to use the DefaultBinder.

returnType
Type

The return type of the property.

types
Type[]

An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-or-

An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

modifiers
ParameterModifier[]

An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. The default binder does not process this parameter.

Returns

An object representing the property that matches the specified requirements, if found; otherwise, null.

Implements

Exceptions

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

name is null.

-or-

types is null.

types is multidimensional.

-or-

modifiers is multidimensional.

-or-

types and modifiers do not have the same length.

An element of types is null.

Remarks

For more information about this API, see Supplemental API remarks for Type.GetProperty.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, Type, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Searches for the specified public property whose parameters match the specified argument types and modifiers.

C#
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
C#
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);

Parameters

name
String

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

returnType
Type

The return type of the property.

types
Type[]

An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-or-

An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

modifiers
ParameterModifier[]

An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. The default binder does not process this parameter.

Returns

An object representing the public property that matches the specified requirements, if found; otherwise, null.

Implements

Exceptions

More than one property is found with the specified name and matching the specified argument types and modifiers.

name is null.

-or-

types is null.

types is multidimensional.

-or-

modifiers is multidimensional.

-or-

types and modifiers do not have the same length.

An element of types is null.

Examples

The following example obtains a Type object corresponding to MyPropertyClass, and the indexed property of this class is retrieved using the arguments passed to the GetProperty method.

C#
using System;
using System.Reflection;

public class MyPropertyClass
{
    private readonly int [,] _myPropertyArray = new int[10,10];
    // Declare an indexer.
    public int this [int i,int j]
    {
        get
        {
            return _myPropertyArray[i,j];
        }
        set
        {
            _myPropertyArray[i,j] = value;
        }
    }
}

public class MyTypeClass
{
    public static void Main()
    {
        try
        {
            Type myType=typeof(MyPropertyClass);
            Type[] myTypeArray = new Type[2];

            // Create an instance of the Type array representing the number, order
            // and type of the parameters for the property.
            myTypeArray.SetValue(typeof(int),0);
            myTypeArray.SetValue(typeof(int),1);

            // Search for the indexed property whose parameters match the
            // specified argument types and modifiers.
            PropertyInfo myPropertyInfo = myType.GetProperty("Item",
                typeof(int),myTypeArray,null);
            Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name +
                " has a property type of " + myPropertyInfo.PropertyType);
         }
        catch(Exception ex)
        {
            Console.WriteLine("An exception occurred " + ex.Message);
        }
    }
}

Remarks

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

Although the default binder does not process ParameterModifier (the modifiers parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers. ParameterModifier is only used when calling through COM interop, and only parameters that are passed by reference are handled.

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

If the current 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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Searches for the specified public property whose parameters match the specified argument types.

C#
public System.Reflection.PropertyInfo? GetProperty(string name, Type[] types);
C#
public System.Reflection.PropertyInfo GetProperty(string name, Type[] types);

Parameters

name
String

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

types
Type[]

An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-or-

An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

Returns

An object representing the public property whose parameters match the specified argument types, if found; otherwise, null.

Implements

Exceptions

More than one property is found with the specified name and matching the specified argument types.

name is null.

-or-

types is null.

types is multidimensional.

An element of types is null.

Examples

The following example retrieves the Type object of a user-defined class, retrieves the property of that class, and displays the property name and type of the property as specified by the arguments passed to GetProperty.

C#

using System;
using System.Reflection;

class MyClass3
{
    private readonly int[,] _myArray = { { 1, 2 }, { 3, 4 } };
    // Declare an indexer.
    public int this[int i, int j]
    {
        get
        {
            return _myArray[i, j];
        }
        set
        {
            _myArray[i, j] = value;
        }
    }
}

public class MyTypeClass3
{
    public static void Main(string[] args)
    {
        try
        {
            // Get the Type object.
            Type myType = typeof(MyClass3);
            Type[] myTypeArr = new Type[2];

            // Create an instance of a Type array.
            myTypeArr.SetValue(typeof(int), 0);
            myTypeArr.SetValue(typeof(int), 1);

            // Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
            PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);

            // Display the property.
            Console.WriteLine("The {0} property exists in MyClass3.",
                myPropInfo.ToString());
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Source : {0}", e.Source);
            Console.WriteLine("Message : {0}", e.Message);
        }
    }
}

Remarks

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

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

If the current 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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, Type, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Searches for the specified public property whose parameters match the specified argument types.

C#
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types);
C#
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType, Type[] types);

Parameters

name
String

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

returnType
Type

The return type of the property.

types
Type[]

An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-or-

An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

Returns

An object representing the public property whose parameters match the specified argument types, if found; otherwise, null.

Implements

Exceptions

More than one property is found with the specified name and matching the specified argument types.

name is null.

-or-

types is null.

types is multidimensional.

An element of types is null.

Remarks

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

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

If the current 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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr);
C#
public System.Reflection.PropertyInfo GetProperty(string name, System.Reflection.BindingFlags bindingAttr);

Parameters

name
String

The string containing the name of the property to get.

bindingAttr
BindingFlags

A bitwise combination of the enumeration values that specify how the search is conducted.

-or-

Default to return null.

Returns

An object representing the property that matches the specified requirements, if found; otherwise, null.

Implements

Exceptions

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

name is null.

Examples

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.

C#

using System;
using System.Reflection;

class MyClass2
{
    // Declare MyProperty.
    public int MyProperty { get; set; }
}

public class MyTypeClass2
{
    public static void Main(string[] args)
    {
        try
        {
            // Get Type object of MyClass2.
            Type myType = typeof(MyClass2);

            // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
            PropertyInfo myPropInfo = myType.GetProperty(
                "MyProperty",
                BindingFlags.Public | BindingFlags.Instance
                );

            // Display Name property to console.
            Console.WriteLine("{0} is a property of MyClass2.", myPropInfo.Name);
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("MyProperty does not exist in MyClass2." + e.Message);
        }
    }
}

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Searches for the public property with the specified name.

C#
public System.Reflection.PropertyInfo? GetProperty(string name);
C#
public System.Reflection.PropertyInfo GetProperty(string name);

Parameters

name
String

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

Returns

An object representing the public property with the specified name, if found; otherwise, null.

Implements

Exceptions

More than one property is found with the specified name.

name is null.

Examples

The following example retrieves the Type object of a user-defined class, retrieves a property of that class, and displays the property name.

C#
using System;
using System.Reflection;

class MyClass1
{
    // Declare MyProperty.
    public int MyProperty { get; set; }
}

public class MyTypeClass1
{
    public static void Main(string[] args)
    {
        try
        {
            // Get the Type object corresponding to MyClass1.
            Type myType = typeof(MyClass1);

            // 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 MyClass1.", myPropInfo.Name);
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("The property does not exist in MyClass1." + e.Message);
        }
    }
}

Remarks

For more information about this API, see Supplemental API remarks for Type.GetProperty.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, Type)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Searches for the public property with the specified name and return type.

C#
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType);
C#
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType);

Parameters

name
String

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

returnType
Type

The return type of the property.

Returns

An object representing the public property with the specified name, if found; otherwise, null.

Implements

Exceptions

More than one property is found with the specified name.

name is null, or returnType is null.

Examples

The following example defines a class with one property and retrieves the name and type of the property.

C#

using System;
using System.Reflection;

class MyPropertyTypeClass
{
    public string MyProperty1 { get; set; } = "Hello World.";
}

class TestClass
{
    static void Main()
    {
        try
        {
            Type myType = typeof(MyPropertyTypeClass);

            // Get the PropertyInfo object representing MyProperty1.
            PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1", typeof(string));

            Console.WriteLine("The name of the first property of MyPropertyTypeClass is {0}.",
                myStringProperties1.Name);
            Console.WriteLine("The type of the first property of MyPropertyTypeClass is {0}.",
                myStringProperties1.PropertyType);
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException :" + e.Message);
        }
        catch (AmbiguousMatchException e)
        {
            Console.WriteLine("AmbiguousMatchException :" + e.Message);
        }
        catch (NullReferenceException e)
        {
            Console.WriteLine("Source : {0}", e.Source);
            Console.WriteLine("Message : {0}", e.Message);
        }
        //Output:
        //The name of the first property of MyPropertyTypeClass is MyProperty1.
        //The type of the first property of MyPropertyTypeClass is System.String.
    }
}

Remarks

A property is considered public to reflection if it has at least one accessor that is public. Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

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

If the current 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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1