I have a class
class Video
{
public string name;
public string description;
}
And because its exposed through a WCF interface I want to rename it (the business object has now been modified to encapsulate audio and images as well as video).
So my solution was to write another class that inherits from it:
class MediaItem : Video
{
}
We have a ‘factory’ like class that gets a video from the the database.
public Video GetVideo(int videoId)
{
}
However when I call the following:
MediaItem itemToReturn = (MediaItem)contentManagerforPage.GetVideo(mediaId);
I get the error:
Unable to cast object of type ‘Video’ to type ‘MediaItem’.
I understand that I cant do this (cast a baseclass to a subclass). So what is the solution?
I cant expose the class name ‘Video’ through WCF I need to expose MediaItem. Is there any OO approach to this problem (using an interface perhaps)?
If not can I rename the object in WCF attributes?
The default name of a data contract for a given type is the name of that type. To override the default, set the Name property of the DataContractAttribute to an alternative name.
See here and here for more details.