jeudi 24 septembre 2020

Trying to change an item in a private List with reflection

I am wanting to edit an item in a private List from a different class (I can't change it to public). I am trying to use a dynamic to let me modify the 6th item in the list, but I just get errors about having the wrong type though "Failure has occurred while loading a type." I am new to Reflection but it is the only option I can use to try to access the private list.

Here is my code:

dynamic privateList = typeof(Class2).GetField("PrivateList", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(class2);
privateList[5] = new ListObj(2f, 2.5f, ListObj.ListObject6);

Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire