0

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?

Sirolol
  • 77
  • 9
  • 6
    You can't cast something that *isn't* of the target type. We don't know what's creating the object that you're trying to cast, which makes it very hard to help. If you've got some code somewhere using `var dto = new EventDto();` then no, you won't be able to cast that to `EventWithObjectsDto`. You need to find out what's creating the instance. – Jon Skeet Nov 06 '19 at 10:52
  • @JonSkeet please, see my updated question – Sirolol Nov 06 '19 at 10:59
  • 2
    Right, so it's behaving exactly as expected - you're creating something that *isn't* an `EventWithObjectsDto` and trying to cast it to a `EventWithObjectsDto`. That's simply not going to work. Without more context about what you're trying to achieve and why - for example, why you're not creating an `EventWithObjectsDto` to start with - we're not going to be able to help you any further. This is C# working exactly as designed. – Jon Skeet Nov 06 '19 at 11:15
  • its like, trying to be a parent of your parent which is not possible – Anonymous Duck Nov 06 '19 at 11:17

2 Answers2

2

The object being cast must be of type EventWithObjectsDto (or derived type).

Here is a little snipped to demonstrate that if it was the cast would be a successful Event.

var a = new EventWithObjectsDto {Type = "Tip"};
EventDto b = a;
var c = (EventWithObjectsDto) b;
Console.WriteLine(c.Type);

Output is:

Tip

Making an EventWithObjectsDto from EventDto.

While you cannot cast an object of class EventDto to EventWithObjectsDto, you can always create an object of derived type from an object of the parent type.

class EventWithObjectsDto {
    ...
    public EventWithObjectsDto(EventDto e ){(...)}
}


(...)

var simpleEvent = new EventDto();
var simpleEventWithObjects = new EventWithObjectsDto(simpleEvent);

Here is a note form ECMA-334 5th Edition / December 2017C# Language Specification that is relevant.

Reference conversions, implicit or explicit, never change the referential identity of the object being converted. [Note:In other words, while a reference conversion can change the type of the reference, it never changes the type or value of the object being referred to. end note]

tymtam
  • 31,798
  • 8
  • 86
  • 126
2

You can't cast EventDto to EventWithObjectsDto.

Because in OOP theory, EventDto is a generalized type and EventWithObjectsDto is a specialized type.

You can cast EventWithObjectsDto to EventDto because EventWithObjectsDto is larger than EventDto: it includes all EventDto operations and variables.

You can't cast EventDto to EventWithObjectsDto because EventDto is smaller than EventWithObjectsDto: it does not includes all EventWithObjectsDto operations and variables.

It is like you want to say when a cat is an animal that all animals are cats: you can't, it is false. Cats are animals but all animals are not cats.

So you have a type mismatch.

Generalization, Specialization, and Inheritance

Generalization and Specialization - What is the differences

Upcasting and downcasting in C#

What is polymorphism

All you can do is to use a pattern that copies what have an instance of a child class in an instance of a parent class by checking what variables are common (name and type).