C#反射:如何设置嵌套列表的嵌套属性 [英] C# reflection : How to set nested Property of nested Lists

查看:288
本文介绍了C#反射:如何设置嵌套列表的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想像一下路径":

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

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

MyProperty的类型为String. 在编译时,这些类型都不是已知的,在运行时通过汇编的反映是未知的.

MyProperty is of type String. None of the types is known at compiletime, only runtime via reflection of assembly.

我拥有一切,但是我无法使用SetValue在第一个列表上设置第二个列表.我总是得到null-ref异常或目标类型不匹配".

I have everything, but i can not set the second list on the first with SetValue. I always get either null-ref exceptions or "target type does not match".

到目前为止我尝试过的事情:

What I have tried so far:

迭代1:

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

迭代2:

同样,也可以.现在我们有了MyObject.MyFirstList [0] .MySecondList []

Same way, works as well. So now we have MyObject.MyFirstList[0].MySecondList[]

迭代3:TODO:创建MySecondList的第一个对象的实例,并将其MyProperty-property设置为创建的属性:

Iteration 3: TODO: Create instance of first object of MySecondList and set its MyProperty-property to the created property:

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

所以总结一下这个问题:

So to summarize the question:

这有效:

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);

数组有点不同.如果使用数组,则Reflection希望您在使用propertyinfo.SetValue时写下最终值,这是整个数组的值.为此,您可以使用Array.CreateInstance(typeof(arrayType), arrayLength)创建一个新数组,并使用'SetValue(object,index)'方法设置其内容.

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. 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.

这篇关于C#反射:如何设置嵌套列表的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆