I have an item in a collection I need to modify using reflection - I'm using reflection because I don't know the exact type of the generic collection until runtime.
I know how to use the SetValue() method to set the value of a property retrieved through collection, but can I use SetValue() to set an actual object inside a collection?
IEnumerable businessObjectCollection = businessObject as IEnumerable;
foreach (Object o in businessObjectCollection)
{
// I want to set the "o" here to some replacement object in the collection if
// a property on the object equals something
Type t = o.GetType();
PropertyInfo identifierProperty = o.GetType().GetProperty("Identifier");
long entityID = (long)identifierProperty.GetValue(o, null);
if (replacementEntity.Identifier == entityID)
{
// IN THIS CASE: set "o" to be the replacementEntity object
// defined somewhere else.
// I can retrieve the object itself using this code, but
// would I set the object with SetValue?
object item = businessObjectCollection.GetType().GetMethod("get_Item").Invoke(businessObjectCollection, new object[] { 1 });
}
}
Rather than attempt to modify the enumerable, you could replace it with a new enumerable that performs the replacement inline. It really depends what you're doing with it afterwards though so YMMV.
Well, you use
get_Item
to retrieve it so you should be able to callset_Item
to set it:Note that this will explode if the collection is not of a type supporting indexed access.
This method adds an object to a collection property of said objects on an object.
obj is the Parent object which contains a property Collection
propertyName is the name of the Collection property
value is the object that is to be added to the Collection