列出自定义类成员和类型

时间:2013-08-14 14:29:48

标签: c#

这似乎是最基本的事情,但不知怎的,我找不到答案,也无法弄明白。

假设我有一个自定义类:

public class WineCellar
{
   public string year;
   public string wine;
   public double nrbottles;
}

现在我想要一个功能:

WineCellar ex = new WineCellar();
ex.members();

这应该返回:年份,葡萄酒,nrbootles。

 ex.members().types();

应该返回:string,string,double

我想同样的说明,假设你有一个实例{2010,Rioja,6}。是否有通过索引返回这些语法的语法?即。

ex[1] 

ex.{1}

返回2010年?

对不起基本问题。

3 个答案:

答案 0 :(得分:3)

正如米歇尔在评论中所说,这听起来像是解决更大问题的错误方法。

但是,如果你确实需要这种东西,你可以得到使用反射:

//returns a list of propertyInfo objects for the class 
// with all kinds of usefull information
public List<PropertyInfo> GetMemberInfos()
{
    return this.GetType().GetProperties().ToList();
}

//returns a list of property names
public List<string> GetMemberNames
{
    return this.GetType().GetProperties().Select(pi => pi.Name).ToList();
}

//returns a list of names of the property types
public List<string> GetMemberTypeNames
{
    return this.GetType().GetProperties().Select(pi => pi.PropertyType.Name).ToList();
}

//indexer that uses the property name to get the value
//since you are mixing types, you can't get more specific than object
public object this[string property]
{
  get { return this.GetType().GetProperty(property).GetValue(this); }
  set { this.GetType().GetProperty(property).SetValue(this, value); }
}

//indexer that uses the property index in the properties array to get the value
public object this[int index]
{
  get { return this.GetType().GetProperties()[index].GetValue(this); }
  set { this.GetType().GetProperties()[index].SetValue(this, value); }
}

请注意,所有这些方法都非常慢,因为通常反射很慢。您可以尝试缓存一些东西以加快速度。

此外,最后一种方法是 彻头彻尾的危险 。它将(尝试)读取和写入没有保证顺序的数组。事实上,documentation指定:

  

GetProperties方法不返回特定属性   订单,例如字母或声明订单。你的代码一定不能   取决于返回属性的顺序,因为那样   订单各不相同。

例如,如果您将班级更改为:

public class WineCellar
{
  public string year;
  public string region;
  public string wine;
  public double nrbottles;
}

并且您已经习惯使用winecellar[1] = "Pinot Noir",它很可能现在更新region属性,而不是wine属性。

答案 1 :(得分:1)

这是你实现Members方法的方法(如果你想将属性名称作为字符串)

public List<string> Members()
{
   List<string> propNames = new List<string>();
   foreach (var prop in typeof(WineCellar).GetProperties())
   {
       propNames.Add(prop.Name);
   }
   return propNames;
}

这就是你如何实现类型(在相同的情况下)

public List<string> Types()
{
   List<string> propTypes = new List<string>();
   foreach (var prop in typeof(WineCellar).GetProperties())
   {
       propTypes.Add(prop.PropertyType.ToString());
   }
   return propTypes ;
}

最后一件事如果你想获得像ex [n]这样的参数值,你可以在你的类中创建一个简单的索引器

public string this[int n]
{
   get
   {
       int current = 0;
       foreach (var prop in typeof(WineCellar).GetProperties())
       {
          if (current == n)
             return prop.GetValue(this, null).ToString();
          current++;
       }
       return null;
   }
}

但是要使这些方法起作用,您应该将变量更改为像这样的属性

public class WineCellar
{
   public string Year { get; set; }
   public string Wine { get; set; }
   public double Nrbottles { get; set; }
}

答案 2 :(得分:0)

您可以使用反射

foreach (var prop in typeof(WineCellar).GetProperties())
            {

                if (prop.PropertyType == typeof(double) || prop.PropertyType == typeof(double?))
                {

                }
            }

获取值,您可以这样做:

prop.GetValue(obj);