Cast PropertyInfo to Collection

别等时光非礼了梦想. 提交于 2019-12-25 17:42:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!