C#的反思和获取属性

C#的反思和获取属性

本文介绍了C#的反思和获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的哑类结构,我试图找出如何从PeopleList类人的每个实例获取属性。我知道如何从人们的单个实例获得的属性,但不能为我的生活弄清楚如何从PeopleList得到它。我相信这是真的简单,但有人可以点我在正确的方向?



 公共类实例
{
酒店的公共级人物
{
私人字符串_name;
公共字符串名称
{
{返回_name; }
集合{_name =价值; }
}

私人诠释_age;
公众诠释年龄
{
{返回_age; }
集合{_age =价值; }
}

公众人物()
{

}

公众人物(字符串名称,诠释岁)
{
this._name =名称;
this._age =年龄;
}
}

公共类PeopleList:列表<人与GT;
{
公共静态无效DoStuff()
{
PeopleList newList =新PeopleList();

//做一些东西

newList.Add(新人们(蒂姆,35));
}
}
}


解决方案

不过不是100%确定你想要什么,但代码(未经测试),这种快速的位可能让你在正确的轨道上(或至少有助于澄清你想要的)。

 无效ReportValue(字符串作为propName,对象propValue); 

无效ReadList< T>(列表< T>清单)
{
VAR道具= typeof运算(T).GetProperties();
的foreach(列表中的项目牛逼)
{
的foreach(以道具VAR道具)
{
ReportValue(prop.Name,prop.GetValue(项目));
}
}
}



C#应能正常工作指出,从列表,并处理好的,但如果你需要有'PeopleList为泛型类型,那么这应该工作PeopleList'继承:

 无效ReadList< T>(T表),其中T:System.Collections.IList 
{
的foreach(在列表对象的项目)
{
。VAR道具= item.GetType()的GetProperties();
的foreach(以道具VAR道具)
{
ReportValue(prop.Name,prop.GetValue(项目,NULL));
}
}
}

请注意,这将实际过程在列表中,以及在派生类型的属性。


I have the following dummy class structure and I am trying to find out how to get the properties from each instance of the class People in PeopleList. I know how to get the properties from a single instance of People but can't for the life of me figure out how to get it from PeopleList. I am sure this is really straightforward but can someone point me in the right direction?

public class Example
{
    public class People
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public People()
        {

        }

        public People(string name, int age)
        {
            this._name = name;
            this._age = age;
        }
    }

    public class PeopleList : List<People>
    {
        public static void DoStuff()
        {
             PeopleList newList = new PeopleList();

            // Do some stuff

             newList.Add(new People("Tim", 35));
        }
    }
}
解决方案

Still not 100% sure of what you want, but this quick bit of code (untested) might get you on the right track (or at least help clarify what you want).

void ReportValue(String propName, Object propValue);

void ReadList<T>(List<T> list)
{
  var props = typeof(T).GetProperties();
  foreach(T item in list)
  {
    foreach(var prop in props)
    {
      ReportValue(prop.Name, prop.GetValue(item));
    }
  }
}

c# should be able to work out that 'PeopleList' inherits from 'List' and handle that fine, but if you need to have 'PeopleList' as the generic type, then this should work:

void ReadList<T>(T list) where T : System.Collections.IList
{
  foreach (Object item in list)
  {
    var props = item.GetType().GetProperties();
    foreach (var prop in props)
    {
      ReportValue(prop.Name, prop.GetValue(item, null));
    }
  }
}

Note that this will actually process properties in derived types within the list as well.

这篇关于C#的反思和获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:19