c# foreach (property in object)… Is there a simple way of doing this?

前端 未结 9 2050
感情败类
感情败类 2020-11-28 22:10

I have a class containing several properties (all are strings if it makes any difference).
I also have a list, which contains many different instances of the class.

9条回答
  •  温柔的废话
    2020-11-28 22:43

    Sure, no problem:

    foreach(object item in sequence)
    {
        if (item == null) continue;
        foreach(PropertyInfo property in item.GetType().GetProperties())
        {
            // do something with the property
        }
    }
    

提交回复
热议问题