Yes, we can create instance of its base types from derived
class instance. But only constraint is the Base class should not be abstract class or interface.
Using Type.BaseType and Activator.CreateInstance, we can achieve above
functionality.
Here sample code:
class Program
{
static void Main(string[]
args)
{
C
objC = new C();
Type
baseBType = objC.GetType().BaseType; // gets it’s
parent type, here it is class B type
Type
baseAType = baseBType.BaseType; // again get parent
for B type, which is A type
object
objA = Activator.CreateInstance(baseAType); // create a instance uisng Type.
}
}
class A
{
public int I;
}
class B : A
{
public int J;
}
class C : B
{
public int K;
}