System.Type.GetProperty 方法 (String, Type)

方法描述

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

语法定义(C# System.Type.GetProperty 方法 (String, Type) 的用法)

public PropertyInfo GetProperty(
	string name,
	Type returnType
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
name System-String 包含要获取的公共属性名的字符串。
returnType System-Type 属性的返回类型。
返回值 System.Reflection.PropertyInfo 表示具有指定名称的公共属性的对象(如果找到的话);否则为 null。

提示和注释

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

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

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

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

索引器和默认属性

Visual Basic 2005、Visual C# 2005 和 Visual C++ 2005 具有用来访问索引属性的简化语法,并允许将一个索引属性设为其类型的默认属性。 例如,如果变量 myList 引用 ArrayList,则语法 myList[3] (在 Visual Basic 中为 myList(3))将检索索引为 3 的元素。 可以重载该属性。

在 C# 中,此功能称为索引器,而且不能按名称引用。 默认情况下,C# 索引器在元数据中显示为具有“Item”名称的索引属性。 但是,类库开发人员可以使用 IndexerNameAttribute 特性来更改元数据中索引器的名称。 例如,String 类具有一个名为 Chars 的索引器。使用 C# 以外的语言创建的索引属性可以具有“Item”以外的名称。

若要确定某个类型是否具有默认属性,请使用 GetCustomAttributes(Type, Boolean) 方法来测试 DefaultMemberAttribute 特性。 如果该类型具有 DefaultMemberAttribute,则 MemberName 属性返回默认属性的名称。

System.Type.GetProperty 方法 (String, Type)例子

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

using System;
using System.Reflection;

class MyClass1
{
    String myMessage="Hello World.";
    public string MyProperty1
    {
        get
        {			
            return myMessage;
        }
        set
        {
            myMessage =value;
        }			
    }
}
class TestClass
{
    static void Main()
    {
        try
        {	
            Type myType = typeof(MyClass1);
            // Get the PropertyInfo object representing MyProperty1. 
            PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1",
                typeof(string));
            Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name);
            Console.WriteLine("The type of the first property of MyClass1 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 MyClass1 is MyProperty1.
	//The type of the first property of MyClass1 is System.String.
    }
}

异常

异常 异常描述
AmbiguousMatchException 找到不止一个具有指定名称的属性。
ArgumentNullException name 为 null 或 returnType 为 null。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。