本文整理汇总了C#中System.Reflection.PropertyInfo.GetValue方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetValue方法的具体用法?C# PropertyInfo.GetValue怎么用?C# PropertyInfo.GetValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
}
输出:
Type is: Planet Properties (N = 2): Name (String): Jupiter Distance (Double): 365000000
示例2: Main
//引入命名空间
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();
}
}
输出:
Character in position 0: 'a' Character in position 6: 'g' Character in position 25: 'z' The entire string: abcdefghijklmnopqrstuvwxyz