C# 使用 PropertyInfo.GetValue()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1355090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PropertyInfo.GetValue()
提问by Mass Dot Net
I have a class that creates a static array of all properties, using a static constructor. I also have a function -- GetNamesAndTypes() -- that lists the name & type of each property in that array.
我有一个类,它使用静态构造函数创建所有属性的静态数组。我还有一个函数——GetNamesAndTypes()——它列出了该数组中每个属性的名称和类型。
Now I want to create another instance-level function -- GetNamesAndTypesAndValues() -- that displays the name & type of each property in the class, as well as that instance's value. How would I do that? Here's the code that I've written so far:
现在我想创建另一个实例级函数——GetNamesAndTypesAndValues()——它显示类中每个属性的名称和类型,以及该实例的值。我该怎么做?这是我到目前为止编写的代码:
//StaticTest.cs
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
namespace StaticTest
{
public class ClassTest
{
private string m_A, m_B, m_C;
private static PropertyInfo[] allClassProperties;
static ClassTest()
{
Type type = typeof(ClassTest);
allClassProperties = type.GetProperties();
// Sort properties alphabetically by name
// (http://www.csharp-examples.net/reflection-property-names/)
Array.Sort(allClassProperties, delegate(PropertyInfo p1, PropertyInfo p2)
{
return p1.Name.CompareTo(p2.Name);
});
}
public int A
{
get { return Convert.ToInt32(m_A); }
set { m_A = value.ToString(); }
}
public string B
{
get { return m_B; }
set { m_B = value; }
}
public DateTime C
{
get { return DateTime.ParseExact("yyyyMMdd", m_C,
CultureInfo.InvariantCulture); }
set { m_C = String.Format("{0:yyyyMMdd}", value); }
}
public static void GetNamesAndTypes()
{
foreach (PropertyInfo propertyInfo in allClassProperties)
{
Console.WriteLine("{0} [type = {1}]", propertyInfo.Name,
propertyInfo.PropertyType);
}
}
public void GetNamesAndTypesAndValues()
{
foreach (PropertyInfo propertyInfo in allClassProperties)
{
Console.WriteLine("{0} [type = {1}]", propertyInfo.Name,
propertyInfo.PropertyType);
}
}
}
}
//Program.cs
using System;
using System.Collections.Generic;
using StaticTest;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("[static] GetNamesAndTypes()");
ClassTest.GetNamesAndTypes();
Console.WriteLine("");
ClassTest classTest = new ClassTest();
classTest.A = 4;
classTest.B = @"bacon";
classTest.C = DateTime.Now;
Console.WriteLine("[instance] GetNamesAndTypesAndValues()");
classTest.GetNamesAndTypesAndValues();
Console.ReadLine();
}
}
}
I tried using propertyInfo.GetValue(), but I couldn't get it to work.
我尝试使用 propertyInfo.GetValue(),但我无法让它工作。
采纳答案by Yannick Motton
In your example propertyInfo.GetValue(this, null)
should work. Consider altering GetNamesAndTypesAndValues()
as follows:
在你的例子中propertyInfo.GetValue(this, null)
应该有效。考虑更改GetNamesAndTypesAndValues()
如下:
public void GetNamesAndTypesAndValues()
{
foreach (PropertyInfo propertyInfo in allClassProperties)
{
Console.WriteLine("{0} [type = {1}] [value = {2}]",
propertyInfo.Name,
propertyInfo.PropertyType,
propertyInfo.GetValue(this, null));
}
}