【问题标题】:Using PropertyInfo.GetValue()使用 PropertyInfo.GetValue()
【发布时间】:2009-08-30 22:30:57
【问题描述】:

我有一个使用静态构造函数创建所有属性的静态数组的类。我还有一个函数——GetNamesAndTypes()——列出了该数组中每个属性的名称和类型。

现在我想创建另一个实例级函数——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();
        }
    }
}

我尝试使用 propertyInfo.GetValue(),但无法正常工作。

【问题讨论】:

    标签: c# .net reflection


    【解决方案1】:

    在您的示例中,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));
      }
    }
    

    【讨论】:

    • 虽然对于普通的简单属性很好,但索引器属性会失败,索引器属性采用 PropertyInfo.GetIndexParameters 指定的非空参数列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多