G
Guest
I have a situation where i have a base class and a sub-class. A null instance of the sub-class is passed into a function. The function needs to create a new instance of the sub-class, but the function only has knowledge of the base class, not the sub-class
So is there a way to get the function to create a new instance of the sub-class without actually using 'new SubClass()
Let me demonstrate what i want to do. Below, in my situation, the function CreateInstanceOfClass cant actually declare a new instance of SubClass, because it has no knowledge of it. But it still needs to create a new instance of it. If i create a new instance of BaseClass, non of SubClass's overridden functions will get called
public class BaseClas
public virtual int MyFunction() {return 0;
public class SubClass : BaseClas
public override int MyFunction() {return 3;
public class DoStuf
public void CreateInstanceOfClass(BaseClass subClass
//Need to create an instance of SubClass, but cant actually declare a new SumeSubClas
subClass = new typeof(subClass)
int ret = subClass.MyFunction(); //I want 3, not
Any Ideas?
So is there a way to get the function to create a new instance of the sub-class without actually using 'new SubClass()
Let me demonstrate what i want to do. Below, in my situation, the function CreateInstanceOfClass cant actually declare a new instance of SubClass, because it has no knowledge of it. But it still needs to create a new instance of it. If i create a new instance of BaseClass, non of SubClass's overridden functions will get called
public class BaseClas
public virtual int MyFunction() {return 0;
public class SubClass : BaseClas
public override int MyFunction() {return 3;
public class DoStuf
public void CreateInstanceOfClass(BaseClass subClass
//Need to create an instance of SubClass, but cant actually declare a new SumeSubClas
subClass = new typeof(subClass)
int ret = subClass.MyFunction(); //I want 3, not
Any Ideas?