2

这似乎是有史以来最基本的事情,但不知何故我找不到答案,也想不通。

假设我有一个自定义类:

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

现在我想要一个功能:

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

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

和:

 ex.members().types();

应该返回:字符串、字符串、双精度

我猜在同一个注释上,假设您有一个实例 {2010, Rioja, 6}。是否有通过索引返回这些的语法?IE

ex[1] 

或者

ex.{1}

返回 2010 年?

对不起,基本问题。

4

3 回答 3

3

As Michelle said in the comments, this sounds like a wrong approach to a bigger problem.

However, if you do need this kind of things, you can get the using reflection:

//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); }
}

Note that all of these methods are very slow, because in general, reflection is slow. You can try to cache some thing to speed it up.

Also, the last method is downright dangerous. It will (try to) read and write to an array that does not have a guaranteed order. In fact, the documentation specifies:

The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.

For example, if you change your class to:

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

and you were used to using winecellar[1] = "Pinot Noir" that will most likely now update the region property, instead of the wine property.

于 2013-08-14T14:46:23.730 回答
1

这就是您实现成员方法的方式(如果您希望将属性名称作为字符串)

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; }
}
于 2013-08-14T14:37:11.770 回答
0

你可以使用反射

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

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

                }
            }

要获得价值,您可以执行以下操作:

prop.GetValue(obj);
于 2013-08-14T14:33:27.397 回答