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
返回指定对象的属性值。
public:
 System::Object ^ GetValue(System::Object ^ obj);public object GetValue (object obj);public object? GetValue (object? obj);member this.GetValue : obj -> objPublic 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
用索引化属性的可选索引值返回指定对象的该属性值。
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[] -> objPublic Overridable Function GetValue (obj As Object, index As Object()) As Object参数
- obj
- Object
将返回其属性值的对象。
- index
- Object[]
索引化属性的可选索引值。 索引化属性的索引从零开始。 对于非索引化属性,该值应为 null。
返回
指定对象的属性值。
实现
例外
该对象与目标类型不匹配,或者某属性是实例属性但 obj 为 null。
注意:在 .NET for Windows 应用商店应用 或 可移植类库中,请改为 catch Exception 。
              index 中的参数数量与索引属性采用的参数数量不匹配。
试图非法访问类中的私有或受保护方法。
注意:在 .NET for Windows 应用商店应用 或 可移植类库中,请改为捕获基类异常 MemberAccessException。
检索属性值时出错。 例如,为一个索引属性指定的索引值超出范围。 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方法提供了一个BindingFlags实现,Binder参数Default设置为 null,CultureInfo并将 设置为 null。
由于静态属性属于 类型,而不是单个对象,因此通过将 作为对象参数传递 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 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且非公共成员的授予集仅限于调用方授权集或其子集,则此方法可用于访问非公共成员。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
GetValue(Object, BindingFlags, Binder, Object[], CultureInfo)
- 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 -> objPublic 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 来获取静态属性。 例如,使用以下代码获取 的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 开始,如果调用方已使用 标志授予ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess调用方,并且非公共成员的授予集仅限于调用方授权集或其子集,则此方法可用于访问非公共成员。 (请参阅 Reflection.) 若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。