简体   繁体   English

如何在C#中获取对象的差异属性?

[英]How to get difference properties of object in c#?

i have a class with some properties like this: 我有一类具有这样的属性:

public class Car 
    {
        public long No { get; set; }
        public string Name { get; set; }
        public int Door { get; set; }
        public Color Color { get; set; }
        public int MaxSpeed { get; set; }
        public int Price { get; set; }
    } 

(this class is an example, my real class is very bigger than it.) (该类是一个例子,我的真实类比它大得多。)

In my program, I need to get difference properties of this class from db(not all properties each time). 在我的程序中,我需要从db获取此类的不同属性(并非每次都获得所有属性)。 For example in one case I need name and color of some cars, and in other case I need name and door and price . 例如,在一种情况下,我需要某些汽车的名称颜色 ,在另一种情况下,我需要名称价格 I want to create one method that support all difference conditions. 我想创建一种支持所有不同条件的方法。 I know that i could create it using 'params' and 'enum' , but I am research about best way to do it. 我知道我可以使用'params''enum'来创建它,但是我正在研究实现它的最佳方法。 Thanks 谢谢

You can just query the propertie when it is called. 您只能在调用属性时对其进行查询。

public int Value{
      get{
         int myValue = getValue();
         return myValue;
      }
}

Try do this way: 尝试这样做:

public object[] GetProperties(int state)
{
 object[] temp;
 switch(state)
 {
    case(0):
    {
        temp=new object[]{Name,Color};
    }break;
    case(1):
    {
        temp=new object[]{Name,door};
    }
 }
}

After that, you know , what need return your function, and it's easy parse return result! 之后,您知道了什么需要返回您的函数,并且很容易解析返回结果!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM