开发者

How can I delete the many-to-many relationship between two dynamic objects?

开发者 https://www.devze.com 2023-04-01 14:11 出处:网络
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\

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消