c# – System.Reflection – 如何调用次级类型的getter方法?
我有三节课:
public class TestA { public string Str1 { get; set; } public string Str2 { get; set; } public TestB TestB { get; set; } public TestA() { Str1 = "string1"; Str2 = "string2"; TestB = new TestB(); } } public class TestB { public string Str3 { get; set; } public string Str4 { get; set; } public TestC ObjTestC { get; set; } public TestB() { Str3 = "string3"; Str4 = "string4"; ObjTestC = new TestC(); } } public class TestC { public string Str5 { get; set; } public TestC() { Str5 = "string5"; } } 现在,我已经获得了所有PropertyInfo并创建了一个新对象: //Get all the properties var prop = typeof(TestA).GetProperties(); for (int i = 0; i < prop.Count(); i++) { var propertyInfo = prop[i]; if (propertyInfo.PropertyType.Namespace != "System") { if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) { Type itemType = propertyInfo.PropertyType.GetGenericArguments()[0]; // use this... var listObjectProperties = itemType.GetProperties(); prop = prop.Union(listObjectProperties).ToArray(); } else { var childProp = propertyInfo.PropertyType.GetProperties(); prop = prop.Union(childProp).ToArray(); } } } //Create Object TestA testA = new TestA(); 现在,我需要的是调用每个属性的getter方法.我尝试了以下调用TestA类属性的getter.但是,当尝试在TestB和TestC中调用属性的getter时,它会抛出错误: // Loop through all properties foreach (PropertyInfo propertyInfo in prop) { MethodInfo getterMethodInfo = propertyInfo.GetGetMethod(); var obj=getterMethodInfo.Invoke(testA,null); } 请帮忙… 提前致谢 解决方法
检查这段代码,我希望prop数组包含TestA和TestB上的所有属性,并且根本不接触TestC:
// Gets TestA's properties... var prop = typeof(TestA).GetProperties(); // Then gets TestB's properties... var childProp = propertyInfo.PropertyType.GetProperties(); prop = prop.Union(childProp).ToArray(); 如果你想从这三种类型中获取属性,你应该递归地执行它.每次更改这些对象的结构时,您都不希望必须修改此循环. 但这不是真正的问题. 真正的问题是针对TestB中的属性调用了getterMethodInfo.Invoke(testA,null),但是传递了TestA的实例,因此出现了错误.想想如果不使用反射,你将如何获得这个值.您现有的代码基本上是这样做的: var value = testA.Str3; // !!! there is no Str3 property on TestA !!! 要正常工作,您需要这样做: var value = testA.TestB.Str3; 看看我们如何调用一系列属性来获得所需的价值?首先是TestA.TestB,然后是TestB.Str3.如果你需要使用反射,那么代码需要做同样的事情: var obj = (object)testA; obj = testBPropOnTestA.GetValue(obj,null); var value = str3PropOnTestB.GetValue(obj,null); 因此,您需要存储PropertyInfos列表,而不是在集合中存储PropertyInfo的简单实例. 所以你的最终“获取值”循环看起来更像是: foreach (var propertyChain in prop) { var obj = (object)testA; foreach (var property in propertyChain) { obj = property.GetValue(obj,null); if (obj == null) { break; } } Console.WriteLine("{0} = {1}",string.Join(".",propertyChain.Select(x => x.Name)),obj); } 我会留意如何修复第一个“收集PropertyInfos”循环给你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |