PropertyInfo.GetValue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回指定物件的屬性值。
多載
| GetValue(Object) |
傳回指定物件的屬性值。 |
| GetValue(Object, Object[]) |
傳回具有索引屬性之選擇性索引值之指定對象的屬性值。 |
| GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) |
在衍生類別中覆寫時,傳回具有指定系結、索引和文化特性特定資訊的指定對象的屬性值。 |
GetValue(Object)
傳回指定物件的屬性值。
public:
System::Object ^ GetValue(System::Object ^ obj);
public object GetValue (object obj);
public object? GetValue (object? obj);
member this.GetValue : obj -> obj
Public Function GetValue (obj As Object) As Object
參數
- obj
- Object
將傳回其屬性值的物件。
傳回
指定物件的屬性值。
範例
下列範例會定義具有兩個屬性的 Planet 類別:Name,行星的名稱;和 Distance,行星與地球的距離。 此範例會具現化代表木星行星的 Planet 物件,並將它傳遞給 GetPropertyValues 方法,以顯示屬性的相關信息,並使用 GetValue 方法來取得每個 Planet 屬性的值。
using System;
using System.Reflection;
public class Planet
{
private String planetName;
private Double distanceFromEarth;
public Planet(String name, Double distance)
{
planetName = name;
distanceFromEarth = distance;
}
public String Name
{ get { return planetName; } }
public Double Distance
{ get { return distanceFromEarth; }
set { distanceFromEarth = value; } }
}
public class Example
{
public static void Main()
{
Planet jupiter = new Planet("Jupiter", 3.65e08);
GetPropertyValues(jupiter);
}
private static void GetPropertyValues(Object obj)
{
Type t = obj.GetType();
Console.WriteLine("Type is: {0}", t.Name);
PropertyInfo[] props = t.GetProperties();
Console.WriteLine("Properties (N = {0}):",
props.Length);
foreach (var prop in props)
if (prop.GetIndexParameters().Length == 0)
Console.WriteLine(" {0} ({1}): {2}", prop.Name,
prop.PropertyType.Name,
prop.GetValue(obj));
else
Console.WriteLine(" {0} ({1}): <Indexed>", prop.Name,
prop.PropertyType.Name);
}
}
// The example displays the following output:
// Type is: Planet
// Properties (N = 2):
// Name (String): Jupiter
// Distance (Double): 365000000
Imports System.Reflection
Public Class Planet
Private planetName As String
Private distanceFromEarth As Double
Public Sub New(name As String, distance As Double)
planetName = name
distanceFromEarth = distance
End Sub
Public ReadOnly Property Name As String
Get
Return planetName
End Get
End Property
Public Property Distance As Double
Get
Return distanceFromEarth
End Get
Set
distanceFromEarth = value
End Set
End Property
End Class
Module Example
Public Sub Main()
Dim jupiter As New Planet("Jupiter", 3.65e08)
GetPropertyValues(jupiter)
End Sub
Private Sub GetPropertyValues(obj As Object)
Dim t As Type = obj.GetType()
Console.WriteLine("Type is: {0}", t.Name)
Dim props() As PropertyInfo = t.GetProperties()
Console.WriteLine("Properties (N = {0}):",
props.Length)
For Each prop In props
If prop.GetIndexParameters().Length = 0 Then
Console.WriteLine(" {0} ({1}): {2}", prop.Name,
prop.PropertyType.Name,
prop.GetValue(obj))
Else
Console.WriteLine(" {0} ({1}): <Indexed>", prop.Name,
prop.PropertyType.Name)
End If
Next
End Sub
End Module
' The example displays the following output:
' Type is: Planet
' Properties (N = 2):
' Name (String): Jupiter
' Distance (Double): 365000000
備註
您可以呼叫 GetValue(Object) 多載來擷取非索引屬性的值;如果您嘗試擷取索引屬性的值,方法會擲回 TargetParameterCountException 例外狀況。 您可以呼叫 GetIndexParameters 方法來判斷屬性是否已編製索引。 如果傳回 ParameterInfo 陣列的長度為零,則不會編製屬性的索引。
這是一個便利的方法,提供抽象 GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) 方法的實作,並將 BindingFlags 參數設定為 BindingFlags.Default、Binder 設定為 null、索引值的物件陣列設定為 null,以及設定為 null的 CultureInfo。
適用於
GetValue(Object, Object[])
傳回具有索引屬性之選擇性索引值之指定對象的屬性值。
public:
virtual System::Object ^ GetValue(System::Object ^ obj, cli::array <System::Object ^> ^ index);
public virtual object GetValue (object obj, object[] index);
public virtual object? GetValue (object? obj, object?[]? index);
abstract member GetValue : obj * obj[] -> obj
override this.GetValue : obj * obj[] -> obj
Public Overridable Function GetValue (obj As Object, index As Object()) As Object
參數
- obj
- Object
將傳回其屬性值的物件。
- index
- Object[]
索引屬性的選擇性索引值。 索引屬性的索引是以零起始。 對於非索引屬性,此值應該 null。
傳回
指定物件的屬性值。
實作
例外狀況
物件不符合目標類型,或屬性是實例屬性,但 objnull。
index 中的參數數目不符合索引屬性採用的參數數目。
嘗試在類別記憶體取私用或受保護的方法時發生非法嘗試。
注意:在適用於 Windows 市集應用程式的 .NET 中, 或 可攜式類別庫,請改為攔截基類例外狀況,MemberAccessException。
擷取屬性值時發生錯誤。 例如,針對索引屬性指定的索引值超出範圍。 InnerException 屬性表示錯誤的原因。
範例
下列範例示範如何取得索引屬性的值。 String.Chars[] 屬性是 String 類別的預設屬性(C# 中的索引器)。
using System;
using System.Reflection;
class Example
{
public static void Main()
{
string test = "abcdefghijklmnopqrstuvwxyz";
// Get a PropertyInfo object representing the Chars property.
PropertyInfo pinfo = typeof(string).GetProperty("Chars");
// Show the first, seventh, and last letters
ShowIndividualCharacters(pinfo, test, 0, 6, test.Length - 1);
// Show the complete string.
Console.Write("The entire string: ");
for (int x = 0; x < test.Length; x++)
{
Console.Write(pinfo.GetValue(test, new Object[] {x}));
}
Console.WriteLine();
}
static void ShowIndividualCharacters(PropertyInfo pinfo,
object value,
params int[] indexes)
{
foreach (var index in indexes)
Console.WriteLine("Character in position {0,2}: '{1}'",
index, pinfo.GetValue(value, new object[] { index }));
Console.WriteLine();
}
}
// The example displays the following output:
// Character in position 0: 'a'
// Character in position 6: 'g'
// Character in position 25: 'z'
//
// The entire string: abcdefghijklmnopqrstuvwxyz
Imports System.Reflection
Module Example
Sub Main()
Dim test As String = "abcdefghijklmnopqrstuvwxyz"
' Get a PropertyInfo object representing the Chars property.
Dim pinfo As PropertyInfo = GetType(String).GetProperty("Chars")
' Show the first, seventh, and last characters.
ShowIndividualCharacters(pinfo, test, { 0, 6, test.Length - 1 })
' Show the complete string.
Console.Write("The entire string: ")
For x As Integer = 0 To test.Length - 1
Console.Write(pinfo.GetValue(test, { x }))
Next
Console.WriteLine()
End Sub
Sub ShowIndividualCharacters(pinfo As PropertyInfo,
value As Object,
ParamArray indexes() As Integer)
For Each index In indexes
Console.WriteLine("Character in position {0,2}: '{1}'",
index, pinfo.GetValue(value, { index }))
Next
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' Character in position 0: 'a'
' Character in position 6: 'g'
' Character in position 25: 'z'
'
' The entire string: abcdefghijklmnopqrstuvwxyz
備註
若要判斷屬性是否已編製索引,請使用 GetIndexParameters 方法。 如果產生的陣列有0(零) 個元素,則不會編製屬性的索引。
這是一個便利的方法,提供抽象 GetValue 方法的實作,其中具有 Default的 BindingFlags 參數、設定為 null的 Binder,以及設定為 null的 CultureInfo。
因為靜態屬性屬於類型,而不是個別物件,因此傳遞 null 做為物件自變數來取得靜態屬性。 例如,使用下列程式代碼取得 CultureInfo 的靜態 CurrentCulture 屬性:
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
若要使用 GetValue 方法,請先取得 類別 Type。 從 Type取得 PropertyInfo。 從 PropertyInfo使用 GetValue 方法。
注意
從 .NET Framework 2.0 開始,如果呼叫端已使用 ReflectionPermissionFlag.RestrictedMemberAccess 旗標授與 ReflectionPermission,而且非公用成員的授與集限制為呼叫端的授與集,或子集,這個方法就可以用來存取非公用成員。 (請參閱反映
適用於
GetValue(Object, BindingFlags, Binder, Object[], CultureInfo)
在衍生類別中覆寫時,傳回具有指定系結、索引和文化特性特定資訊的指定對象的屬性值。
public:
abstract System::Object ^ GetValue(System::Object ^ obj, System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ index, System::Globalization::CultureInfo ^ culture);
public abstract object? GetValue (object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? index, System.Globalization.CultureInfo? culture);
public abstract object GetValue (object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] index, System.Globalization.CultureInfo culture);
abstract member GetValue : obj * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo -> obj
Public MustOverride Function GetValue (obj As Object, invokeAttr As BindingFlags, binder As Binder, index As Object(), culture As CultureInfo) As Object
參數
- obj
- Object
將傳回其屬性值的物件。
- invokeAttr
- BindingFlags
下列列舉成員的位元組合,指定調用屬性:InvokeMethod、CreateInstance、Static、GetField、SetField、GetProperty和 SetProperty。 您必須指定適當的調用屬性。 例如,若要叫用靜態成員,請設定 Static 旗標。
- binder
- Binder
物件,可透過反映啟用系結、強制自變數類型、調用成員,以及擷取 MemberInfo 物件。 如果 bindernull,則會使用預設系結器。
- index
- Object[]
索引屬性的選擇性索引值。 對於非索引屬性,此值應該 null。
- culture
- CultureInfo
要本地化資源的文化特性。 如果資源未針對此文化特性當地語系化,則會連續呼叫 Parent 屬性,以搜尋相符專案。 如果此值是 null,則會從 CurrentUICulture 屬性取得特定文化特性資訊。
傳回
指定物件的屬性值。
實作
例外狀況
物件不符合目標類型,或屬性是實例屬性,但 objnull。
index 中的參數數目不符合索引屬性採用的參數數目。
嘗試在類別記憶體取私用或受保護的方法時發生非法嘗試。
擷取屬性值時發生錯誤。 例如,針對索引屬性指定的索引值超出範圍。 InnerException 屬性表示錯誤的原因。
備註
若要判斷屬性是否已編製索引,請使用 GetIndexParameters 方法。 如果產生的陣列有0(零) 個元素,則不會編製屬性的索引。
因為靜態屬性屬於類型,而不是個別物件,因此傳遞 null 做為物件自變數來取得靜態屬性。 例如,使用下列程式代碼取得 CultureInfo 的靜態 CurrentCulture 屬性:
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
若要使用 GetValue 方法,請先取得 類別 Type。 從 Type取得 PropertyInfo。 從 PropertyInfo使用 GetValue 方法。
注意
從 .NET Framework 2.0 開始,如果呼叫端已使用 ReflectionPermissionFlag.RestrictedMemberAccess 旗標授與 ReflectionPermission,而且非公用成員的授與集限制為呼叫端的授與集,或子集,這個方法就可以用來存取非公用成員。 (請參閱反映