Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert base class to derived class [duplicate]

Is it possible in C# to explicitly convert a base class object to one of it's derived classes? Currently thinking I have to create a constructor for my derived classes that accept a base class object as a parameter and copy over the property values. I don't really like this idea, so I'd like to avoid it if possible.

This doesn't seem like it should work (object is instantiated as new base, so memory shouldn't be allocated for extra members of the derived class) but C# seems to allow me to do it:

class BaseClass {   ... some stuff ... }  class DerivedClass : BaseClass {     public bool MyDerivedProperty{ get; set; } }   static void Main(string[] args) {     BaseClass myBaseObject = new BaseClass();     DerivedClass myDerivedObject = myBaseObject as DerivedClass;      myDerivedObject.MyDerivedProperty = true; } 
like image 327
ARW Avatar asked Sep 24 '12 13:09

ARW


People also ask

Can you cast a base class to a derived class?

Likewise, a reference to base class can be converted to a reference to derived class using static_cast . If the source type is polymorphic, dynamic_cast can be used to perform a base to derived conversion.

How do you assign a base class to a derived class?

You could write a constructor in the derived class taking a base class object as parameter, copying the values. In that case you would copy the base object and get a fully functional derived class object with default values for derived members.

What is Downcasting in C#?

Introduction. This article describes a simple approach to downcasting in C#; downcasting merely refers to the process of casting an object of a base class type to a derived class type. Upcasting is legal in C# as the process there is to convert an object of a derived class type into an object of its base class type.

What does a derived class inherit from the base class?

The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class. A Derived class is also called a child class or subclass.


2 Answers

No, there's no built-in way to convert a class like you say. The simplest way to do this would be to do what you suggested: create a DerivedClass(BaseClass) constructor. Other options would basically come out to automate the copying of properties from the base to the derived instance, e.g. using reflection.

The code you posted using as will compile, as I'm sure you've seen, but will throw a null reference exception when you run it, because myBaseObject as DerivedClass will evaluate to null, since it's not an instance of DerivedClass.

like image 172
Tim S. Avatar answered Oct 11 '22 13:10

Tim S.


That's not possible. but you can use an Object Mapper like AutoMapper

EDIT

I want to suggest a simpler object mapper: TinyMapper. AutoMapper is now very complicated to use. I don't use it anymore. TinyMapper covers most use cases and is far more simple AND super fast.

Example:

//In app startup TinyMapper.Bind<Person, PersonDto>();  //Usage var personDto = TinyMapper.Map<PersonDto>(person); 

Example for AutoMapper (older versions I think) for the ones who still want to use it:

class A {     public int IntProp { get; set; } } class B {     public int IntProp { get; set; }     public string StrProp { get; set; } } 

In global.asax or application startup:

AutoMapper.Mapper.CreateMap<A, B>(); 

Usage:

var b = AutoMapper.Mapper.Map<B>(a); 

It's configurable via a fluent API.

like image 30
Mahmood Dehghan Avatar answered Oct 11 '22 14:10

Mahmood Dehghan