Type.GetProperty 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得目前 Type 的特定屬性。
多載
GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) |
使用指定的繫結條件約束搜尋指定的屬性,而該屬性的參數符合指定的引數類型和修飾詞。 |
GetProperty(String) |
搜尋具有指定名稱的公用屬性。 |
GetProperty(String, BindingFlags) |
使用指定的繫結條件約束搜尋指定的屬性。 |
GetProperty(String, Type) |
搜尋具有指定名稱和傳回類型的公用屬性。 |
GetProperty(String, Type[]) |
搜尋指定的公用屬性,其參數符合指定的引數類型。 |
GetProperty(String, Type, Type[]) |
搜尋指定的公用屬性,其參數符合指定的引數類型。 |
GetProperty(String, Type, Type[], ParameterModifier[]) |
搜尋指定的公用屬性,其參數符合指定的引數類型和修飾詞。 |
GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
使用指定的繫結條件約束搜尋指定的屬性,而該屬性的參數符合指定的引數類型和修飾詞。
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.PropertyInfo? GetProperty (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags, binder As Binder, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo
參數
- name
- String
字串,包含要取得的屬性名稱。
- binder
- Binder
定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。
-或-
Null 參考 (在 Visual Basic 中為Nothing
),可使用 DefaultBinder。
- returnType
- Type
屬性的傳回類型。
- types
- Type[]
Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。
-或- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。
- modifiers
- ParameterModifier[]
ParameterModifier 物件的陣列,代表在 types
陣列中與對應項目關聯的屬性。 預設的繫結器不會處理這個參數。
傳回
代表符合指定之需求屬性的物件 (如有找到);否則為 null
。
實作
例外狀況
找到一個以上的屬性,其具有指定名稱,且符合指定繫結條件約束。
types
的項目是 null
。
備註
如果屬性至少有一個 public 存取子,則會將屬性視為公用。 否則,屬性會被視為私用,而您必須使用 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic 中,使用) 結合值 Or
來取得它。
雖然預設系結器不會處理 ParameterModifier modifiers
參數) (,但您可以使用抽象 System.Reflection.Binder 類來撰寫執行處理常式的自訂 modifiers
系結器。 ParameterModifier
只有在透過 COM interop 呼叫時才會使用,而且只會處理以傳址方式傳遞的參數。
下表顯示在反映型別時,方法會傳回基類的成員 Get
。
成員類型 | Static | 非靜態 |
---|---|---|
建構函式 | 否 | 否 |
欄位 | 否 | 可以。 欄位一律會依名稱和簽章進行隱藏。 |
事件 | 不適用 | 一般類型系統規則是,繼承與實作為屬性的方法相同。 反映會將屬性視為隱藏名稱和簽章。 請參閱下面的附注2。 |
方法 | 否 | 可以。 (虛擬和非虛擬) 的方法都可以依名稱或依名稱、依名稱和簽章來隱藏。 |
巢狀型別 | 否 | 否 |
屬性 | 不適用 | 一般類型系統規則是,繼承與實作為屬性的方法相同。 反映會將屬性視為隱藏名稱和簽章。 請參閱下面的附注2。 |
依名稱和簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、個 sentinel 和非受控呼叫慣例。 這是二進位比較。
針對反映,屬性和事件會依名稱和簽章來隱藏。 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類的 setter。
自訂屬性不是一般型別系統的一部分。
您 BindingFlags 可以使用下列篩選旗標來定義要包含在搜尋中的屬性:
您必須指定或,才能
BindingFlags.Instance
BindingFlags.Static
取得傳回。指定
BindingFlags.Public
要在搜尋中包含公用屬性。指定
BindingFlags.NonPublic
要包含非公用屬性 (也就是在搜尋中) 的私用、內部和受保護的屬性。指定
BindingFlags.FlattenHierarchy
要public
在階層中包含和protected
靜態成員;private
不包含繼承類別中的靜態成員。
您 BindingFlags 可以使用下列修飾詞旗標來變更搜尋的運作方式:
BindingFlags.IgnoreCase
忽略的案例name
。BindingFlags.DeclaredOnly
表示只搜尋在上宣告的屬性 Type ,而不是直接繼承的屬性。
如需相關資訊,請參閱 System.Reflection.BindingFlags 。
如果目前的 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 # 以外的語言所建立的索引屬性,也可以有專案以外的名稱。
若要判斷類型是否有預設屬性,請使用 GetCustomAttributes(Type, Boolean) 方法來測試 DefaultMemberAttribute 屬性。 如果類型為 DefaultMemberAttribute ,則屬性會傳回 MemberName 預設屬性的名稱。
另請參閱
- PropertyInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
適用於
GetProperty(String)
搜尋具有指定名稱的公用屬性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public System.Reflection.PropertyInfo? GetProperty (string name);
public System.Reflection.PropertyInfo GetProperty (string name);
member this.GetProperty : string -> System.Reflection.PropertyInfo
abstract member GetProperty : string -> System.Reflection.PropertyInfo
override this.GetProperty : string -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String) As PropertyInfo
參數
- name
- String
字串,包含要取得的公用屬性名稱。
傳回
物件,代表具有指定之名稱的公用屬性 (如有找到),否則為 null
。
實作
例外狀況
找到一個以上具有指定名稱的屬性。
name
為 null
。
範例
下列範例會抓取 Type
使用者自訂類別的物件,並抓取該類別的屬性,並顯示內容名稱。
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 the Type object corresponding to MyClass.
Type^ myType = MyClass::typeid;
// 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 MyClass.", myPropInfo->Name );
}
catch ( NullReferenceException^ e )
{
Console::WriteLine( "The property does not exist in MyClass. {0}", e->Message );
}
}
using System;
using System.Reflection;
class MyClass
{
private int myProperty;
// Declare MyProperty.
public int MyProperty
{
get
{
return myProperty;
}
set
{
myProperty=value;
}
}
}
public class MyTypeClass
{
public static void Main(string[] args)
{
try
{
// Get the Type object corresponding to MyClass.
Type myType=typeof(MyClass);
// 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 MyClass.", myPropInfo.Name);
}
catch(NullReferenceException e)
{
Console.WriteLine("The property does not exist in MyClass." + e.Message);
}
}
}
Imports System.Reflection
Class MyClass1
Private myProperty1 As Integer
' Declare MyProperty.
Public Property MyProperty() As Integer
Get
Return myProperty1
End Get
Set(ByVal Value As Integer)
myProperty1 = Value
End Set
End Property
End Class
Public Class MyTypeClass
Public Shared Sub Main(ByVal args() As String)
Try
' Get Type Object corresponding to MyClass.
Dim myType As Type = GetType(MyClass1)
' Get PropertyInfo object by passing property name.
Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty")
' Display Name propety to console.
Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name)
Catch e As NullReferenceException
Console.WriteLine("The property does not exist in MyClass.", e.Message.ToString())
End Try
End Sub
End Class
就內部而言,中繼資料會以名稱 "Item" 來參考這個屬性。 任何 PropertyInfo
使用反映的嘗試都必須指定此內部名稱,才能正確地傳回 PropertyInfo
屬性。
備註
搜尋會區分 name
大小寫。 搜尋包含公用靜態和公用實例屬性。
如果屬性至少有一個 public 存取子,則會將屬性視為公用。 否則,屬性會被視為私用,而您必須使用 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic 中,使用) 結合值 Or
來取得它。
如果目前的 Type 代表結構化泛型型別,這個方法會傳回, PropertyInfo 並以適當的型別引數取代型別參數。
如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的屬性。
發生的情況 AmbiguousMatchException 包括下列各項:
類型包含兩個具有相同名稱但參數數目不同的索引屬性。 若要解決不明確的問題,請使用 GetProperty 指定參數類型之方法的多載。
衍生型別會使用
new
Visual Basic) 中的修飾詞 (,宣告隱藏具有相同名稱之繼承屬性的屬性Shadows
。 若要解決不明確的問題,請使用方法多載 GetProperty(String, BindingFlags) ,並新增旗標,將 BindingFlags.DeclaredOnly 搜尋限制為不會繼承的成員。
索引子和預設屬性
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 # 以外的語言所建立的索引屬性,也可以有專案以外的名稱。
若要判斷類型是否有預設屬性,請使用 GetCustomAttributes(Type, Boolean) 方法來測試 DefaultMemberAttribute 屬性。 如果類型為 DefaultMemberAttribute ,則屬性會傳回 MemberName 預設屬性的名稱。
另請參閱
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
適用於
GetProperty(String, BindingFlags)
使用指定的繫結條件約束搜尋指定的屬性。
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public System.Reflection.PropertyInfo? GetProperty (string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags) As PropertyInfo
參數
- name
- String
字串,包含要取得的屬性名稱。
傳回
代表符合指定之需求屬性的物件 (如有找到);否則為 null
。
實作
例外狀況
找到一個以上的屬性,其具有指定名稱,且符合指定繫結條件約束。
name
為 null
。
範例
下列範例會抓取使用者自訂類別的型別、抓取該類別的屬性,並根據指定的系結條件約束來顯示內容名稱。
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 );
}
}
using System;
using System.Reflection;
class MyClass
{
private int myProperty;
// Declare MyProperty.
public int MyProperty
{
get
{
return myProperty;
}
set
{
myProperty=value;
}
}
}
public class MyTypeClass
{
public static void Main(string[] args)
{
try
{
// Get Type object of MyClass.
Type myType=typeof(MyClass);
// 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.Name);
}
catch(NullReferenceException e)
{
Console.WriteLine("MyProperty does not exist in MyClass." +e.Message);
}
}
}
Imports System.Reflection
Module Module1
Public Class MyClass1
Private myProperty1 As Integer
' Declare MyProperty.
Public Property MyProperty() As Integer
Get
Return myProperty1
End Get
Set(ByVal Value As Integer)
myProperty1 = Value
End Set
End Property
Public Shared Sub Main()
Try
' Get a Type object corresponding to MyClass.
Dim myType As Type = GetType(MyClass1)
' Get a PropertyInfo object by passing property name and specifying BindingFlags.
Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty", BindingFlags.Public Or BindingFlags.Instance)
' Display the Name property.
Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name)
Catch e As NullReferenceException
Console.WriteLine("MyProperty does not exist in MyClass.", e.Message.ToString())
End Try
End Sub
End Class
End Module 'Module1
備註
如果屬性至少有一個 public 存取子,則會將屬性視為公用。 否則,屬性會被視為私用,而您必須使用 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic 中,使用) 結合值 Or
來取得它。
您 BindingFlags 可以使用下列篩選旗標來定義要包含在搜尋中的屬性:
您必須指定或,才能
BindingFlags.Instance
BindingFlags.Static
取得傳回。指定
BindingFlags.Public
要在搜尋中包含公用屬性。指定
BindingFlags.NonPublic
要包含非公用屬性 (也就是在搜尋中) 的私用、內部和受保護的屬性。指定
BindingFlags.FlattenHierarchy
要public
在階層中包含和protected
靜態成員;private
不包含繼承類別中的靜態成員。
您 BindingFlags 可以使用下列修飾詞旗標來變更搜尋的運作方式:
BindingFlags.IgnoreCase
忽略的案例name
。BindingFlags.DeclaredOnly
表示只搜尋在上宣告的屬性 Type ,而不是直接繼承的屬性。
如需相關資訊,請參閱 System.Reflection.BindingFlags 。
如果目前的 Type 代表結構化泛型型別,這個方法會傳回, PropertyInfo 並以適當的型別引數取代型別參數。
如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的屬性。
發生的情況 AmbiguousMatchException 包括下列各項:
類型包含兩個具有相同名稱但參數數目不同的索引屬性。 若要解決不明確的問題,請使用 GetProperty 指定參數類型之方法的多載。
衍生型別宣告一個屬性,此屬性會使用
new
Visual Basic) 中的修飾詞 (,隱藏具有相同名稱的繼承屬性Shadows
。 若要解決不明確的問題,請包含, BindingFlags.DeclaredOnly 將搜尋限制為不繼承的成員。
索引子和預設屬性
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 # 以外的語言所建立的索引屬性,也可以有專案以外的名稱。
若要判斷類型是否有預設屬性,請使用 GetCustomAttributes(Type, Boolean) 方法來測試 DefaultMemberAttribute 屬性。 如果類型為 DefaultMemberAttribute ,則屬性會傳回 MemberName 預設屬性的名稱。
另請參閱
- PropertyInfo
- String
- BindingFlags
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
適用於
GetProperty(String, Type)
搜尋具有指定名稱和傳回類型的公用屬性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType);
member this.GetProperty : string * Type -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type) As PropertyInfo
參數
- name
- String
字串,包含要取得的公用屬性名稱。
- returnType
- Type
屬性的傳回類型。
傳回
物件,代表具有指定之名稱的公用屬性 (如有找到),否則為 null
。
實作
例外狀況
找到一個以上具有指定名稱的屬性。
name
為 null
或 returnType
為 null
。
範例
下列範例會定義具有一個屬性的類別,並抓取屬性的名稱和型別。
using namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
String^ myMessage;
public:
property String^ MyProperty1
{
String^ get()
{
return myMessage;
}
void set( String^ value )
{
myMessage = value;
}
}
};
int main()
{
try
{
Type^ myType = MyClass1::typeid;
// Get the PropertyInfo Object* representing MyProperty1.
PropertyInfo^ myStringProperties1 = myType->GetProperty( "MyProperty1", String::typeid );
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 : {0}", e->Message );
}
catch ( AmbiguousMatchException^ e )
{
Console::WriteLine( "AmbiguousMatchException : {0}", 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.
}
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.
}
}
Imports System.Reflection
Class MyClass1
Private myMessage As [String] = "Hello World."
Public Property MyProperty1() As String
Get
Return myMessage
End Get
Set(ByVal Value As String)
myMessage = Value
End Set
End Property
End Class
Class TestClass
Shared Sub Main()
Try
Dim myType As Type = GetType(MyClass1)
' Get the PropertyInfo object representing MyProperty1.
Dim myStringProperties1 As PropertyInfo = myType.GetProperty("MyProperty1", GetType(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.ToString())
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException :" + e.Message.ToString())
Catch e As AmbiguousMatchException
Console.WriteLine("AmbiguousMatchException :" + e.Message.ToString())
Catch e As NullReferenceException
Console.WriteLine("Source : {0}", e.Source.ToString())
Console.WriteLine("Message : {0}", e.Message.ToString())
End Try
'Output:
'The name of the first property of MyClass1 is MyProperty1.
'The type of the first property of MyClass1 is System.String.
End Sub
End Class
備註
如果屬性至少有一個 public 存取子,則會將屬性視為公用。 否則,屬性會被視為私用,而您必須使用 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 # 以外的語言所建立的索引屬性,也可以有專案以外的名稱。
若要判斷類型是否有預設屬性,請使用 GetCustomAttributes(Type, Boolean) 方法來測試 DefaultMemberAttribute 屬性。 如果類型為 DefaultMemberAttribute ,則屬性會傳回 MemberName 預設屬性的名稱。
另請參閱
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
適用於
GetProperty(String, Type[])
搜尋指定的公用屬性,其參數符合指定的引數類型。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
public System.Reflection.PropertyInfo? GetProperty (string name, Type[] types);
public System.Reflection.PropertyInfo GetProperty (string name, Type[] types);
member this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, types As Type()) As PropertyInfo
參數
- name
- String
字串,包含要取得的公用屬性名稱。
- types
- Type[]
Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。
-或- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。
傳回
代表其參數符合指定之引數類型的公用屬性物件 (如有找到),否則為 null
。
實作
例外狀況
找到一個以上具有指定名稱,且符合指定引數類型的屬性。
types
是多維的。
types
的項目是 null
。
範例
下列範例會抓取 Type
使用者自訂類別的物件、抓取該類別的屬性,並顯示內容的屬性名稱和類型,如傳遞給的引數所指定 GetProperty
。
using namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
array<int, 2>^myArray;
public:
property int Item [int, int]
{
// Declare an indexer.
int get( int i, int j )
{
return myArray[ i,j ];
}
void set( int i, int j, int value )
{
myArray[ i,j ] = value;
}
}
};
int main()
{
try
{
// Get the Type object.
Type^ myType = MyClass1::typeid;
array<Type^>^myTypeArr = gcnew array<Type^>(2);
// Create an instance of a Type array.
myTypeArr->SetValue( int::typeid, 0 );
myTypeArr->SetValue( int::typeid, 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 MyClass1.", myPropInfo );
}
catch ( NullReferenceException^ e )
{
Console::WriteLine( "An exception occurred." );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
}
using System;
using System.Reflection;
class MyClass1
{
private 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 MyTypeClass
{
public static void Main(string[] args)
{
try
{
// Get the Type object.
Type myType=typeof(MyClass1);
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 MyClass1.", myPropInfo.ToString());
}
catch(NullReferenceException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Source : {0}" , e.Source);
Console.WriteLine("Message : {0}" , e.Message);
}
}
}
Imports System.Reflection
Module Module1
Class MyClass1
Private myArray As Integer(,) = {{1, 2}, {3, 4}}
' Declare an indexer.
Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
Get
Return myArray(i, j)
End Get
Set(ByVal Value As Integer)
myArray(i, j) = Value
End Set
End Property
End Class
Public Class MyTypeClass
Public Shared Sub Main()
Try
' Get the Type Object.
Dim myType As Type = GetType(MyClass1)
Dim myTypeArr(1) As Type
' Create an instance of a Type array.
myTypeArr.SetValue(GetType(Integer), 0)
myTypeArr.SetValue(GetType(Integer), 1)
' Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
Dim myPropInfo As PropertyInfo = myType.GetProperty("Item", myTypeArr)
' Display the property.
Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString())
Catch e As NullReferenceException
Console.WriteLine("An exception occurred.")
Console.WriteLine("Source : {0}", e.Source.ToString())
Console.WriteLine("Message : {0}", e.Message.ToString())
End Try
End Sub
End Class
End Module 'Module1
備註
如果屬性至少有一個 public 存取子,則會將屬性視為公用。 否則,屬性會被視為私用,而您必須使用 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 # 以外的語言所建立的索引屬性,也可以有專案以外的名稱。
若要判斷類型是否有預設屬性,請使用 GetCustomAttributes(Type, Boolean) 方法來測試 DefaultMemberAttribute 屬性。 如果類型為 DefaultMemberAttribute ,則屬性會傳回 MemberName 預設屬性的名稱。
另請參閱
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
適用於
GetProperty(String, Type, Type[])
搜尋指定的公用屬性,其參數符合指定的引數類型。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType, Type[] types);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types);
member this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type()) As PropertyInfo
參數
- name
- String
字串,包含要取得的公用屬性名稱。
- returnType
- Type
屬性的傳回類型。
- types
- Type[]
Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。
-或- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。
傳回
代表其參數符合指定之引數類型的公用屬性物件 (如有找到),否則為 null
。
實作
例外狀況
找到一個以上具有指定名稱,且符合指定引數類型的屬性。
types
是多維的。
types
的項目是 null
。
備註
如果屬性至少有一個 public 存取子,則會將屬性視為公用。 否則,屬性會被視為私用,而您必須使用 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 # 以外的語言所建立的索引屬性,也可以有專案以外的名稱。
若要判斷類型是否有預設屬性,請使用 GetCustomAttributes(Type, Boolean) 方法來測試 DefaultMemberAttribute 屬性。 如果類型為 DefaultMemberAttribute ,則屬性會傳回 MemberName 預設屬性的名稱。
另請參閱
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
適用於
GetProperty(String, Type, Type[], ParameterModifier[])
搜尋指定的公用屬性,其參數符合指定的引數類型和修飾詞。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo
參數
- name
- String
字串,包含要取得的公用屬性名稱。
- returnType
- Type
屬性的傳回類型。
- types
- Type[]
Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。
-或- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。
- modifiers
- ParameterModifier[]
ParameterModifier 物件的陣列,代表在 types
陣列中與對應項目關聯的屬性。 預設的繫結器不會處理這個參數。
傳回
物件,代表符合指定之需求的公用屬性 (如有找到),否則為 null
。
實作
例外狀況
找到一個以上具有指定名稱,且符合指定引數類型和修飾詞的屬性。
types
的項目是 null
。
範例
下列範例 Type
會取得對應的物件 MyPropertyClass
,並使用傳遞給方法的引數抓取這個類別的索引屬性 GetProperty
。
using namespace System;
using namespace System::Reflection;
public ref class MyPropertyClass
{
private:
array<int, 2>^ myPropertyArray;
public:
property int Item [int, int]
{
// Declare an indexer.
int get( int i, int j )
{
return myPropertyArray[ i,j ];
}
void set( int i, int j, int value )
{
myPropertyArray[ i,j ] = value;
}
}
};
int main()
{
try
{
Type^ myType = MyPropertyClass::typeid;
array<Type^>^myTypeArray = gcnew array<Type^>(2);
// Create an instance of the Type array representing the number, order
// and type of the parameters for the property.
myTypeArray->SetValue( int::typeid, 0 );
myTypeArray->SetValue( int::typeid, 1 );
// Search for the indexed property whose parameters match the
// specified argument types and modifiers.
PropertyInfo^ myPropertyInfo = myType->GetProperty( "Item", int::typeid, myTypeArray, nullptr );
Console::WriteLine( "{0}.{1} has a property type of {2}", myType->FullName, myPropertyInfo->Name, myPropertyInfo->PropertyType );
}
catch ( Exception^ ex )
{
Console::WriteLine( "An exception occurred {0}", ex->Message );
}
}
using System;
using System.Reflection;
public class MyPropertyClass
{
private 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);
}
}
}
Imports System.Reflection
Public Class MyPropertyClass
Private myPropertyArray(9, 9) As Integer
' Declare an indexer.
Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
Get
Return myPropertyArray(i, j)
End Get
Set(ByVal Value As Integer)
myPropertyArray(i, j) = Value
End Set
End Property
End Class
Public Class MyTypeClass
Public Shared Sub Main()
Try
Dim myType As Type = GetType(MyPropertyClass)
Dim myTypeArray(1) As Type
' Create an instance of a Type array representing the number, order
' and type of the parameters for the property.
myTypeArray.SetValue(GetType(Integer), 0)
myTypeArray.SetValue(GetType(Integer), 1)
' Search for the indexed property whose parameters match the
' specified argument types and modifiers.
Dim myPropertyInfo As PropertyInfo = myType.GetProperty("Item", _
GetType(Integer), myTypeArray, Nothing)
Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name + _
" has a property type of " + myPropertyInfo.PropertyType.ToString())
Catch ex As Exception
Console.WriteLine("An exception occurred " + ex.Message.ToString())
End Try
End Sub
End Class
備註
如果屬性至少有一個 public 存取子,則會將屬性視為公用。 否則,屬性會被視為私用,而您必須使用 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (Visual Basic 中,使用) 結合值 Or
來取得它。
雖然預設系結器不會處理 ParameterModifier modifiers
參數) (,但您可以使用抽象 System.Reflection.Binder 類來撰寫執行處理常式的自訂 modifiers
系結器。 ParameterModifier
只有在透過 COM interop 呼叫時才會使用,而且只會處理以傳址方式傳遞的參數。
搜尋會區分 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 # 以外的語言所建立的索引屬性,也可以有專案以外的名稱。
若要判斷類型是否有預設屬性,請使用 GetCustomAttributes(Type, Boolean) 方法來測試 DefaultMemberAttribute 屬性。 如果類型為 DefaultMemberAttribute ,則屬性會傳回 MemberName 預設屬性的名稱。
另請參閱
- PropertyInfo
- String
- DefaultBinder
- ParameterModifier
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)