views:

50

answers:

3

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 than Resource.

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

+2  A: 

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.

Guffa
I only thought it was possible because of point 3 in http://stackoverflow.com/questions/2458025/how-to-properly-downcast-in-c-with-a-swig-generated-interface however I don't seem to have the method getCPtr
Shahin
@Shahin: Yes, it's possible to hack an object to think that it's a different object, but this can easily blow up. For example, the garbage collector occationally moves object in memory, and if it moves an object thinking that it is a different type, it could corrupt other objects, or the memory management itself.
Guffa
Ah, not something I'm prepared to risk then... is this possible by serialising as the desired object and de-serialising?
Shahin
A: 

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

saurabh
A: 

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.

João Angelo