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[])

來源:
Type.cs
來源:
Type.cs
來源:
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

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

binder
Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。

-或-

Null 參考 (在 Visual Basic 中為Nothing ),可使用 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[])

來源:
Type.cs
來源:
Type.cs
來源:
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

範例

下列範例會 Type 取得對應至 MyPropertyClass的物件,並使用傳遞至 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.Instance | BindingFlags.Static (,請使用 Or) 結合值來取得它。

雖然預設系結器不會處理 ParameterModifier 參數 (modifiers) ,但您可以使用抽象 System.Reflection.Binder 類來撰寫會處理 modifiers的自定義系結器。 ParameterModifier 只有在透過 COM Interop 呼叫時,才會使用,而且只會處理以傳址方式傳遞的參數。

搜尋 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.cs
來源:
Type.cs
來源:
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.Instance | BindingFlags.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[])

來源:
Type.cs
來源:
Type.cs
來源:
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.Instance | BindingFlags.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)

來源:
Type.cs
來源:
Type.cs
來源:
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

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

傳回

代表符合指定之需求屬性的物件 (如有找到);否則為 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)

來源:
Type.cs
來源:
Type.cs
來源:
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)

來源:
Type.cs
來源:
Type.cs
來源:
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

實作

例外狀況

找到一個以上具有指定名稱的屬性。

namenullreturnTypenull

範例

下列範例會定義具有一個屬性的類別,並擷取屬性的名稱和類型。

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.Instance | BindingFlags.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