Calling new, when i dont know type.

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
You can't do what you want to do. In CreateInstanceOfClass, you're getting a
BaseClass reference passed in. Now, if you had a real instance of a subclass
passed in, you'd be able to call GetType() on it, and then use
Activator.CreateInstance() to create another instance (though it would be
slow to do so).

'null' doesn't have any associated type, so there's no way for the function
to find out what kind of variable you had it in.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
john said:
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.
 
Hi john,

You might find reflection useful for what you want to do. You need to at
least know the name of the sub class as a string to get the Type you need:

Type mytype = Type.GetType("SubClass, MyAssembly");

or you might know the value of mytype by some other means (a lookup list for
instance)

Then you can invoke the constructor via reflection:

BaseClass subClass = (BaseClass)mytype.InvokeMember("",
BindingFlags.CreateInstance, null, null, new Object[]{});
int ret = subClass .MyFunction(); // will return 3

Cheers

Doug Forster


john said:
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.
 
Hello.

Why not to create an abstract method 'BaseClass CreateInstance( .... pars
.... )' in 'BaseClass' and implement it in each subclass.

Best regards!
 
Back
Top