Type.GetProperty 方法

定义

获取当前 Type 的特定属性。

重载

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

使用指定的绑定约束,搜索参数与指定的自变量类型及修饰符匹配的指定属性。

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

搜索其参数与指定自变量类型及修饰符匹配的指定公共属性。

GetProperty(String, Type[])

搜索其参数与指定自变量类型匹配的指定公共属性。

GetProperty(String, Type, Type[])

搜索其参数与指定自变量类型匹配的指定公共属性。

GetProperty(String, BindingFlags)

使用指定的绑定约束搜索指定属性。

GetProperty(String)

搜索具有指定名称的公共属性。

GetProperty(String, Type)

搜索具有指定名称和返回类型的公共属性。

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

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

使用指定的绑定约束,搜索参数与指定的自变量类型及修饰符匹配的指定属性。

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

参数

name
String

包含要获取的属性名的字符串。

bindingAttr
BindingFlags

枚举值的按位组合,这些值指定如何进行搜索。

若为 Default,则返回 null

binder
Binder

一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。

要使用 Nothing 的空引用(在 Visual Basic 中为 DefaultBinder)。

returnType
Type

属性的返回类型。

types
Type[]

一个 Type 对象数组,表示要获取的索引属性的参数的数目、顺序和类型。

获取未被索引的属性的 Type 类型的空数组(即 Type[] types = new Type[0])。

modifiers
ParameterModifier[]

ParameterModifier 对象的数组,表示与 types 数组中的相应元素关联的特性。 默认的联编程序不处理此参数。

返回

表示符合指定需求的属性的对象(如果找到的话);否则为 null

实现

例外

找到多个具有指定名称的属性且属性与指定绑定约束匹配。

namenull

typesnull

types 是多维的。

modifiers 是多维的。

typesmodifiers 的长度不相同。

types 的元素为 null

注解

有关此 API 的详细信息,请参阅 Type.GetProperty 的补充 API 备注

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

搜索其参数与指定自变量类型及修饰符匹配的指定公共属性。

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

参数

name
String

包含要获取的公共属性名的字符串。

returnType
Type

属性的返回类型。

types
Type[]

一个 Type 对象数组,表示要获取的索引属性的参数的数目、顺序和类型。

获取未被索引的属性的 Type 类型的空数组(即 Type[] types = new Type[0])。

modifiers
ParameterModifier[]

ParameterModifier 对象的数组,表示与 types 数组中的相应元素关联的特性。 默认的联编程序不处理此参数。

返回

表示符合指定要求的公共属性的对象(如果找到的话);否则为 null

实现

例外

找到多个具有指定名称且与指定参数类型和修饰符匹配的属性。

namenull

typesnull

types 是多维的。

modifiers 是多维的。

typesmodifiers 的长度不相同。

types 的元素为 null

示例

下面的示例获取对应于 TypeMyPropertyClass的 对象,并使用传递给 GetProperty 方法的参数检索此类的索引属性。

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

注解

如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 Or) 组合这些值来获取它。

尽管默认联编程序不会处理 ParameterModifier (modifiers 参数) ,但可以使用抽象 System.Reflection.Binder 类编写处理 的 modifiers自定义绑定器。 ParameterModifier 仅在通过 COM 互操作调用时才使用 ,并且仅处理由引用传递的参数。

搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。

如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo ,并将类型参数替换为相应的类型参数。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

搜索其参数与指定自变量类型匹配的指定公共属性。

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

参数

name
String

包含要获取的公共属性名的字符串。

types
Type[]

一个 Type 对象数组,表示要获取的索引属性的参数的数目、顺序和类型。

获取未被索引的属性的 Type 类型的空数组(即 Type[] types = new Type[0])。

返回

表示其参数与指定参数类型匹配的公共属性的对象(如果找到的话);否则为 null

实现

例外

找到多个具有指定名称且与指定自变量类型匹配的属性。

namenull

typesnull

types 是多维的。

types 的元素为 null

示例

以下示例检索 Type 用户定义类的 对象,检索该类的 属性,并显示传递给 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);
        }
    }
}

注解

如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 Or) 组合这些值来获取它。

搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。

如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo ,并将类型参数替换为相应的类型参数。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

搜索其参数与指定自变量类型匹配的指定公共属性。

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

参数

name
String

包含要获取的公共属性名的字符串。

returnType
Type

属性的返回类型。

types
Type[]

一个 Type 对象数组,表示要获取的索引属性的参数的数目、顺序和类型。

获取未被索引的属性的 Type 类型的空数组(即 Type[] types = new Type[0])。

返回

表示其参数与指定参数类型匹配的公共属性的对象(如果找到的话);否则为 null

实现

例外

找到多个具有指定名称且与指定自变量类型匹配的属性。

namenull

typesnull

types 是多维的。

types 的元素为 null

注解

如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 Or) 组合这些值来获取它。

搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。

如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo ,并将类型参数替换为相应的类型参数。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

使用指定的绑定约束搜索指定属性。

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

参数

name
String

包含要获取的属性名的字符串。

bindingAttr
BindingFlags

枚举值的按位组合,这些值指定如何进行搜索。

若为 Default,则返回 null

返回

表示符合指定需求的属性的对象(如果找到的话);否则为 null

实现

例外

找到多个具有指定名称的属性且属性与指定绑定约束匹配。

namenull

示例

以下示例检索用户定义的类的类型,检索该类的属性,并根据指定的绑定约束显示属性名称。

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

搜索具有指定名称的公共属性。

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

参数

name
String

包含要获取的公共属性名的字符串。

返回

表示具有指定名称的公共属性的对象(如果找到的话);否则为 null

实现

例外

找到了多个具有指定名称的属性。

namenull

示例

以下示例检索 Type 用户定义类的 对象,检索该类的属性,并显示属性名称。

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

注解

有关此 API 的详细信息,请参阅 Type.GetProperty 的补充 API 备注

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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

搜索具有指定名称和返回类型的公共属性。

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

参数

name
String

包含要获取的公共属性名的字符串。

returnType
Type

属性的返回类型。

返回

表示具有指定名称的公共属性的对象(如果找到的话);否则为 null

实现

例外

找到了多个具有指定名称的属性。

namenull,或 returnTypenull

示例

以下示例定义一个具有一个属性的类,并检索该属性的名称和类型。

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

注解

如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 Or) 组合这些值来获取它。

搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。

如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo ,并将类型参数替换为相应的类型参数。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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