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
.