使用PropertyInfo.GetValue() [英] Using PropertyInfo.GetValue()

查看:191
本文介绍了使用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 (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();
		}
	}
}



我试着用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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆