I want to delete the many-to-many relationship between two dynamic entities.
I've seen examples using dummy objects, but they were not dynamic. I will not know the object or the name of the objects' collection navigation properties until run-time. So I can't just say,
apple.Oranges.Remove(orange)
I need to do it dynamically. Something like,
dynamicModel.开发者_C百科dynamicCollection(collectionName).Remove(otherDynamicModel)
I don't need extension methods necessarily, just something that gets the job done. How can I do this? Thanks.
(I don't know what other details might be helpful to provide since the objects are dynamic?)
I think you're best off using reflection in this case still:
((dynamic)dynamicModel.GetType().GetProperty(collectionName)
.GetValue(dynamicModel, null))
.Remove(otherDynamicModel)
Or if you know it will be an IList
((IList)dynamicModel.GetType().GetProperty(collectionName)
.GetValue(dynamicModel, null))
.Remove(otherDynamicModel)
精彩评论