问题
How do I cast a collection property in order to manage it? Please, take a look the attached image. I need to get the collection by it's name, and then work with this collection to add, remove, etc.
回答1:
You can't cast a PropertyInfo
(which is just metadata information about the property) to its value type; you have to go through the GetValue
method.
That method will take an instance to retrieve from or null if it's static. The result is an object
that you then cast:
ICollection collection = (ICollection)myCollectionProperty.GetValue(myEntity);
来源:https://stackoverflow.com/questions/46573870/cast-propertyinfo-to-collection