i've 2 classes called EventDto
and EventWithObjectsDto
. EventDto
is the parent class and EventWithObjectsDto
inherits from it.
These are the classes:
public class EventDto : BaseDto
{
public EventTypes Type { get; set; }
public string Title { get; set; }
public DateTime StartAt { get; set; }
public DateTime EndAt { get; set; }
public Guid? RecursiveCriteriaId { get; set; }
}
public class EventWithObjectsDto : EventDto
{
public List<ObjectDto> Objects { get; set; }
}
I want to cast EventDto
to EventWithObjectsDto
, so i've tryed 2 ways:
var eventWithObjects = (EventWithObjectsDto)ev;
that throws InvalidCastException
and var eventWithObjects = ev as EventWithObjectsDto
that returns null
.
The only way that works is to remove : EventDto
from EventWithObjectsDto
and use AutoMapper to map EventDto
to EventWithObjectsDto
but i don't want to use AutoMapper is exists another way.
Also, i've done a simple test:
var simpleEvent = new EventDto();
// Thrown InvalidCastException
var simpleEventWithObjects = (EventWithObjectsDto)simpleEvent;
Can someone help me?