lundi 28 juin 2021

Modify collection items using reflection?

I'd like to use reflection to find a collection by name (MyAssembly.Constants.MyCollection), check its items (List<string>), and add an item if it doesn't already exist. So far I'm able to find the collection and see its items, but need help adding the missing value to the real collection. Here's what I have so far:

var assembly = Assembly.Load("MyAssembly");
var constants = assembly.GetType("MyAssembly.Constants");
var myCollection = constants.GetField("MyCollection").GetValue(null) as List<string>;

if (!myCollection.Contains("some_value"))
{
     //Add the missing item to MyAssembly.Constants.MyCollection
}




Aucun commentaire:

Enregistrer un commentaire