Is it possible to convert to a derived class without data elements in C #?

I am trying to make a shell using inheritance. I actually work with a de-serialization code that has very common names, and I want to save a few keystrokes on a method that wraps an internal object.

public class Base { public string Foo { get; set; } } public class Derived : Base { public string Bar { get { return this.Foo; } } } Base base = new Base(); Derived d = (Derived)base; 

I get an error message that is trying to compress. Is this possible in C #? There is no extra data in the derived class, so my C ++ brain tells me that downgrade is possible ...

+4
source share
1 answer

Nope. The class identifier is determined not only by the data layout (and that true in C ++ is also for non-POD types).

You might want to learn extension methods to add additional features that do not require additional data storage.

+8
source

All Articles