简体   繁体   English

如何访问 object 类型 c# 的属性?

[英]How to access properties of the object type c#?

var obj1 = new A()
{
  Name = "abc",
  Id = 1
}

var obj2 = new B()
{
   Place = "XYZ",
   Pincode = 123456
}
var obj3 = new C()
{
   Mark = 100,
   Standard = "Tenth"
}

var myList = new List<object>();
myList .add(obj1);
myList .add(obj2);
myList .add(obj3);

This is my code structure.这是我的代码结构。 I need to access the properties of the myList object.我需要访问myList object 的属性。 ie) I need to access the properties like Name , Id , Place , Pincode , Mark , Standard from the myList object and it's corresponding values.即)我需要访问myList object 中的NameIdPlacePincodeMarkStandard等属性及其对应的值。

How to achieve it?如何实现?

You can try something like below.您可以尝试以下方法。 Working code here工作代码在这里

public static List<List<string>> GetProperties(List<object> myList) 
{
    //  If you don't want two lists, you can use Dictionary<key, value>
    List<string> props = new List<string>();
    List<string> values = new List<string>();

    foreach (var a in myList)
    {
        if(a == null)
            continue;

        var propsInfo = a.GetType().GetProperties();
        foreach (var prop in propsInfo)
        {
            if(!props.Contains(prop.Name))
            {
                props.Add(prop.Name);
                values.Add(prop.GetValue(a, null).ToString());
            }
        }
    }
    return new List<List<string>> { props, values};
}

As I wrote in my comment, usually keeping completely different types in the same collection is wrong.正如我在评论中所写,通常在同一个集合中保留完全不同的类型是错误的。 However, that doesn't mean that's always the case, and so assuming you have a good enough reason to do that - here's one option to do it.但是,这并不意味着总是如此,因此假设您有足够的理由这样做 - 这是一种选择。
Assuming c# 7 or higher, your best option would probably be to (ab)use switch with pattern matching :假设 c# 7 或更高版本,您最好的选择可能是(ab)使用带有模式匹配的开关

foreach(var obj in myList)
{
    switch(obj)
    {
        case A a:
            DoSomethingWithA(a);
            break;
        case B b:
            DoSomethingWithB(b);
            break;
    } 
}

Here is some code I use to compare two objects.这是我用来比较两个对象的一些代码。 I don't know if this is your scenario but I hope it helps you get what you need it for.我不知道这是否是您的情况,但我希望它可以帮助您获得所需的东西。

protected List<string> GetProperties(List<object> myList) 
{
    List<string> props = new List<string>();
    
    foreach (var a in myList)
    {
       if(a == null)
         continue;
        
       var propsInfo = a.GetType().GetProperties();
       foreach (var prop in propsInfo)
       {
          if(!props.Contains(prop.Name))
          {
             props.Add(prop.Name);
          }
       }
    }
    return props;
}

If you are simply looking to get values out of an object (property values), that is a different story.如果您只是想从 object(属性值)中获取值,那就另当别论了。

foreach (objectType obj in mylist)
{
    someVariable1 = obj.name;
    someVariable2 = obj.Id;
}

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

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