使用PropertyInfo.GetValue()

编程入门 行业动态 更新时间:2024-10-28 14:33:09
本文介绍了使用PropertyInfo.GetValue()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个创建所有属性的静态数组,使用静态构造函数的类。我也有一个功能 - GetNamesAndTypes() - 列出名称和放大器; 。类型的数组中的每个属性的

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.

现在我想创建另一个实例级功能 - GetNamesAndTypesAndValues​​() - 显示名字和放大器;类型类中的每个属性,以及该实例的价值。我会怎么做呢?下面是我到目前为止已经写的代码:

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:

//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 (www.csharp-examples/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(); } } }

我试着用propertyInfo.GetValue (),但我不能得到它的工作。

I tried using propertyInfo.GetValue(), but I couldn't get it to work.

推荐答案

在您的例子 propertyInfo.GetValue (这一点,空)应该工作。考虑修改 GetNamesAndTypesAndValues​​()如下:

In your example propertyInfo.GetValue(this, null) should work. Consider altering GetNamesAndTypesAndValues() as follows:

public void GetNamesAndTypesAndValues() { foreach (PropertyInfo propertyInfo in allClassProperties) { Console.WriteLine("{0} [type = {1}] [value = {2}]", propertyInfo.Name, propertyInfo.PropertyType, propertyInfo.GetValue(this, null)); } }

更多推荐

使用PropertyInfo.GetValue()

本文发布于:2023-11-03 07:02:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PropertyInfo   GetValue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!