No, It is Immpossible. In a managed language like C #, this just won't work. Runtime will not allow this, even if the compiler skips it.
You yourself said it seemed stupid:
SomeBaseClass class = new SomeBaseClass(); SomeDerivedClass derClass = (SomeDerivedClass)class;
So ask yourself, is there a class instance of SomeDerivedClass ? No, therefore, the conversion does not make sense. If you need to convert SomeBaseClass to SomeDerivedClass , then you must provide some kind of conversion, either a constructor or a conversion method.
It sounds as if your class hierarchy needs some work. In general, it should not be possible to convert an instance of a base class into an instance of a derived class. Usually there should be data and / or functionality that does not belong to the base class. If the functionality of a derived class is applied to all instances of the base class, then it must either be collapsed into the base class or be part of a new class that is not part of the base class hierarchy.
Derek Park Sep 23 '08 at 22:53 2008-09-23 22:53
source share