I have the classes:
[DataContract]
public class MediaItem : Video
{
//used to protect calling api from the incorrectly named video
}
[DataContract]
public class SecureMediaItem : MediaItem
{
[DataMember]
public PlayerEmbedToken Token;
}
Then I have the following code:
List<MediaItem> dummyItems = new List<MediaItem>();
//cast the media item into a secure media item
SecureMediaItem retItem = (SecureMediaItem)dummyItems[mediaId];
//TODO: put in the Admin ID
retItem.Token.UserId = 1;
retItem.Token.IpAddress = VLSCore2.VlsSecurity.ParseIpFromString(ipAddress);
retItem.Token.UniquePlayerRef = Guid.NewGuid().ToString();
return retItem;
However Im getting an error:
Unable to cast object of type
‘VLSCore2.Entities.Security.Api.MediaItem’ to type
‘VLSCore2.Entities.Security.Api.SecureMediaItem’.
This is just simply a widending cast is it not? Something stopping it like the DataContract attribute?
No. It’s the opposite of a widening cast.
SecureMediaItemis more specific thanMediaItem. You could cast aSecureMediaItemto aMediaItem, because aSecureMediaItemis aMediaItem. The other way around isn’t working, because aMediaItemisn’t necessarily aSecureMediaItem.