Imagine following "path" :
MyObject.MyFirstList[0].MySecondList[0].MyProperty = "Hello"
MyProperty is of type 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. I always get either null-ref exceptions or "target type does not match".
What I have tried so far:
iteration 1:
var constructedList = Activator.CreateInstance(constructedListType);
target.GetType().GetProperty(propertyToAdd).SetValue(target, constructedList)
Iteration 2:
Same way, works as well. So now we have MyObject.MyFirstList[0].MySecondList[]
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:
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(...);