Consider following problem: I want to fill an array of type T with values at runtime. I am able to get all the GameObjects (talking about Unity3D here) which hold one of these instances. These GameObjects are in the results array. The GetComponent method allows to extract the instances of T from the gameObject, but autmatically casts it to Ts base class Component. blueprint contains information about the binding target, like the FieldInfo of the array.
The current code looks like this:
var arrayOfBaseClass =
(
from instance
in results
select instance.GetComponent(blueprint.Field.FieldType.GetElementType()))
.ToArray();
// Missing Step here
blueprint.Field.SetValue(o, arrayOfBaseClass);
SetValue throws right now ArgumentException: Object type UnityEngine.Component[] cannot be converted to target type: VEL.Input.ActionSignalReceiver[]
Additional info arrayOfBaseClass is actually an array of instaces of T, just cast to the base type by GetComponent. If I knew T beforehand a manual cast to T would work and solve this problem. Sadly T is not know beforehand and may vary between different types (yet all derive from Component)
The question now is if there is a way to use the information of FieldInfo to cast the array back to its most derived type.