I have 2 static classes that hold collections of items that are each used for separate areas in the system. Here is the issue I am having:
When I use reflection to add an item of type X to one of the static classes, which each hold their own collection of type X, the item gets added to the collection of each static class, when I only want it to be added to one of my static classes.
Before the following lines are executed:
//FirstStaticClass.TypedObjects.Count: 0;
//SecondStaticClass.TypedObjects.Count: 0;
Retrieving the PropertyInfo of my target static class:
var collectionPropertyInfo = typeof(FirstStaticClass).GetProperties().Single(x=> x.GetCustomeAttributes.Any(y=> y.GetType() == typeof(AttributeIAmLookingFor));
Adding my object to the collection retrieved above:
collectionPropertyInfo.GetValue(null).GetType().GetMethod("Add").Invoke(collectionPropertyInfo.GetValue(null), new[] {objectToAdd});
Immediately after the above line executes, I can inspect each of my static classes and they each contain the item:
//FirstStaticClass.TypedObjects.Count: 1;
//SecondStaticClass.TypedObjects.Count: 1;
In the above example, my static classes each have a collection of the same type, and are named the same. I thought that the reflection was finding all properties with the same name, so I tried changing it. However, even with a different name, the collection on the SecondStaticClass would contain the added object:
//FirstStaticClass.TypedObjects.Count: 1;
//SecondStaticClass.DifferentNameSameType.Count: 1;
I searched for a while without being able to find anything relating to my specific issue. Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire