开发者

Upcasting without retaining reference to derived type

开发者 https://www.devze.com 2023-01-16 01:11 出处:网络
I have a class called Resource, this is inherited by a class called ResourceMeta I need to upcast ResourceMeta to Resource without it still thinking it is a type of ResourceMeta.

I have a class called Resource, this is inherited by a class called ResourceMeta

I need to upcast ResourceMeta to Resource without it still thinking it is a type of ResourceMeta.

When I try to save my object using entity framework, it will complain about mappings not existing, rightly so because it will be trying to save ResourceMeta rather th开发者_C百科an Resource.

I've tried (Resource)resourceMeta however this still retains the type of ResourceMeta just limits the properties available to Resource.


That is not possible, you can't change the actual type of an object.

To get a Resource object from a ResourceMeta object, you have to create a new Resource object using the data from the ResourceMeta object.


Not possible in simple words , you need to create a new instance of Resource and then return


Is inheritance the correct representation of the association between ResourceMeta and Resource instances. If not, you could favor composition instead of inheritance and in this way when you want to save the resource instance you could just do resourceMetaInstance.Resource.

0

精彩评论

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

关注公众号