简体   繁体   English

C#reflection:如何设置嵌套列表的嵌套属性

[英]C# reflection : How to set nested Property of nested Lists

Imagine following "path" : 想象一下“路径”:

MyObject.MyFirstList[0].MySecondList[0].MyProperty = "Hello" MyObject.MyFirstList [0] .MySecondList [0] .MyProperty =“Hello”

MyProperty is of type String. MyProperty的类型为String。 None of the types is known at compiletime, only runtime via reflection of assembly. 在编译时不知道任何类型,只有运行时通过反射汇编。

I have everything, but i can not set the second list on the first with SetValue. 我拥有一切,但我无法使用SetValue在第一个列表中设置第二个列表。 I always get either null-ref exceptions or "target type does not match". 我总是得到null-ref异常或“目标类型不匹配”。

What I have tried so far: 到目前为止我尝试过的:

iteration 1: 迭代1:

var constructedList = Activator.CreateInstance(constructedListType);
target.GetType().GetProperty(propertyToAdd).SetValue(target, constructedList)

Iteration 2: 迭代2:

Same way, works as well. 同样的方式也适用。 So now we have MyObject.MyFirstList[0].MySecondList[] 所以现在我们有MyObject.MyFirstList [0] .MySecondList []

Iteration 3: TODO: Create instance of first object of MySecondList and set its MyProperty-property to the created property: 迭代3:TODO:创建MySecondList的第一个对象的实例,并将其MyProperty属性设置为created属性:

var target = Activator.CreateInstance(typeOfItem);
target.GetType().GetProperty(propertyToAdd)?.SetValue(target, null);

So to summarize the question: 总结一下这个问题:

This works: 这有效:

someInstance.GetType().GetProperty(someProperty).SetValue(someInstance, objToSet);

Why does something like this not work? 为什么这样的事情不起作用? Or does it - if yes, how? 或者它 - 如果是的话,怎么样?

someInstance.GetType().GetProperty(someList.listInsideSomeList.finalProperty).SetValue(...);

In short, reflection provides you with means to get information on a type. 简而言之,反射为您提供了获取类型信息的方法。 It cannot be used to reference the property of another type (as shown in your last example). 它不能用于引用其他类型的属性(如上一个示例所示)。

You are on the right track, but you need to go from right to left in your path. 你是在正确的轨道上,但你需要在你的道路上从右到左。 So taken you have some nested properties. 所以你需要一些嵌套属性。 You first create the nested object, such as: 首先创建嵌套对象,例如:

var objNested = Activator.CreateInstance(objNestedType);
objNestedType.GetProperty(nestedPropertyName).SetValue(objNested, value);

and then you add your newly created object to its 'parent': 然后将新创建的对象添加到其“父”:

var objBase = Activator.CreateInstance(objBaseType);
objBaseType.GetProperty(basePropertyName).SetValue(objBase, objNested);

Arrays are bit different. 数组有点不同。 Reflection expects you to write down the final value when using propertyinfo.SetValue , in case of an array this is the entire array value. Reflection希望您在使用propertyinfo.SetValue时记下最终值,如果是数组,则这是整个数组值。 In order to do this you can use Array.CreateInstance(typeof(arrayType), arrayLength) to create a new array and use the 'SetValue(object, index)' method to set its contents. 为此,您可以使用Array.CreateInstance(typeof(arrayType), arrayLength)创建一个新数组,并使用'SetValue(object,index)'方法设置其内容。

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

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