如何使用c#中的反射读取包含数组类型元素的对象的属性。如果我有一个名为GetMyProperties的方法,并且我确定该对象是自定义类型,那么我该如何读取数组的属性及其中的值。 IsCustomType是确定类型是否为自定义类型的方法。
public void GetMyProperties(object obj)
{
foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
{
if (!Helper.IsCustomType(pinfo.PropertyType))
{
string s = pinfo.GetValue(obj, null).ToString();
propArray.Add(s);
}
else
{
object o = pinfo.GetValue(obj, null);
GetMyProperties(o);
}
}
}
场景是,我有一个ArrayClass和ArrayClass的对象有两个属性:
-string Id
-DeptArray[] depts
DeptArray是另一个具有2个属性的类:
-string code
-string value
因此,此方法获取ArrayClass的对象。我想在字典/列表项中读取所有属性从上到下并存储名称/值对。我能够为价值,定制,枚举类型做到这一点。我被一堆物体困住了。不知道怎么做。