PropertyInfo.GetValue 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定对象的属性值。
重载
| 名称 | 说明 |
|---|---|
| GetValue(Object) |
返回指定对象的属性值。 |
| GetValue(Object, Object[]) |
返回具有索引属性的可选索引值的指定对象的属性值。 |
| GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) |
在派生类中重写时,返回具有指定绑定、索引和区域性特定信息的指定对象的属性值。 |
GetValue(Object)
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
返回指定对象的属性值。
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的对象数组以及 CultureInfo 设置为 null该对象数组。
适用于
GetValue(Object, Object[])
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
返回具有索引属性的可选索引值的指定对象的属性值。
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 适用于非索引属性。
返回
指定对象的属性值。
实现
例外
对象与目标类型不匹配,或者属性是实例属性,但 obj 为 null。
中的 index 参数数与索引属性采用的参数数不匹配。
尝试在类中非法访问私有或受保护的方法。
检索属性值时出错。 例如,为索引属性指定的索引值已超过范围。 该 InnerException 属性指示错误的原因。
示例
以下示例演示如何获取索引属性的值。 该 String.Chars[] 属性是类的默认属性(C# String 中的索引器)。
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参数、Binder一个设置为null和CultureInfo一个参数null。
由于静态属性属于类型(而不是单个对象),因此通过作为对象参数传递 null 来获取静态属性。 例如,使用以下代码获取静态 CurrentCulture 属性 CultureInfo :
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
若要使用该方法 GetValue ,请先获取类 Type。 从中 Type获取 PropertyInfo。 从中PropertyInfoGetValue,使用该方法。
注释
如果使用标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且非公共成员的授予集仅限于调用方授予集或子集,则此方法可用于访问非公共成员。 (请参阅反射的安全注意事项。)若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
GetValue(Object, BindingFlags, Binder, Object[], CultureInfo)
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
- Source:
- PropertyInfo.cs
在派生类中重写时,返回具有指定绑定、索引和区域性特定信息的指定对象的属性值。
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 对象。
binder如果是null,则使用默认绑定器。
- index
- Object[]
索引属性的可选索引值。 此值应 null 适用于非索引属性。
- culture
- CultureInfo
要为其本地化资源的区域性。 如果未为此区域性本地化资源, Parent 则会在搜索匹配项时连续调用该属性。 如果此值是 null,则从 CurrentUICulture 属性获取特定于区域性的信息。
返回
指定对象的属性值。
实现
例外
对象与目标类型不匹配,或者属性是实例属性,但 obj 为 null。
中的 index 参数数与索引属性采用的参数数不匹配。
尝试在类中非法访问私有或受保护的方法。
检索属性值时出错。 例如,为索引属性指定的索引值已超过范围。 该 InnerException 属性指示错误的原因。
注解
若要确定属性是否已编制索引,请使用 GetIndexParameters 该方法。 如果生成的数组具有 0(零)个元素,则不会为该属性编制索引。
由于静态属性属于类型(而不是单个对象),因此通过作为对象参数传递 null 来获取静态属性。 例如,使用以下代码获取静态 CurrentCulture 属性 CultureInfo :
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
若要使用该方法 GetValue ,请先获取类 Type。 从中 Type获取 PropertyInfo。 从中PropertyInfoGetValue,使用该方法。
注释
如果使用标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且非公共成员的授予集仅限于调用方授予集或子集,则此方法可用于访问非公共成员。 (请参阅反射的安全注意事项。)若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。