mercredi 26 mai 2021

C# Reflection : How do I access an array's value if it is an object?

Let's say if we have a class containing an array:

public class MyClass<T> : BaseClass
{
    public T[] ObjectArray = new T[100];
}

And I want to access the ObjectArray value by reflections:

// instance1 = MyClass<string>
// instance2 = MyClass<int>
var list = new List<BaseClass>{ instance1, instance2 };
foreach(var element in list)
{
    var arrObj = element.GetType().GetField("ObjectArray").GetValue(element);
    //...and what to do now if I want to access the value in ObjectArray ?
}




Aucun commentaire:

Enregistrer un commentaire