I am doing something like the answer on: Set object property using reflection
Dynamically setting the value of a object property. I have a function wrapping this sort of functionality and it works great. However, I want to make it so that it looks at the property type to see if it is some sort of collection and add the value/object to the collection.
I tried to do something like: if (object is ICollection)
The problem is that VS2010 wants me to type the collection which I dont know how to do programatically.
So what I want to do is something like the following given subject
is the target object and value
is value to be set:
public void setPropertyOnObject(object subject, string Name, object value)
{
var property = subject.GetType().GetProperty(Name)
// -- if property is collection ??
var collection = property.GetValue(subject, null);
collection.add(value)
// -- if propert is not a collection
property.SetValue(subject, value, null);
}