Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an instance with given name?

Status
Not open for further replies.

juhaka

Programmer
Nov 14, 2002
26
FI
Hi!
I have several classes, which inherited the same class and implemented the same interface. And now I want to define in runtime, which instance is created (the name of the class is given in a setting -file).

How I do it? Is there 'Class.forName("xxx")' -thing or that...

br.
Juha Ka
 
Don't understand this question..

YourClassInstance.GetType();

????
 
I mean, I hava an interface,
public interface IFoo{
public void methodA();
}

and I have an abstract class, which implements the interface
public abstract class CFoo:IFoo{
public abstract void methodA();
}
.

And then, I have several classes, which inherited this abstract class:
public class MyClass1:CFoo{
void methodA(){
impl_1
}
}


public class MyClass2:CFoo{
void methodA(){
impl_2
}

}


public class MyClass3:CFoo{
void methodA(){
impl_3 }

}


So, I have now three different implementation for IFoo -interface. And now I want define in runtime (via settings -file), which one I use. So, I should create an instance by using the name of the class(MyClass1, MyClass2 or myClass3). I think, it goes like: CFoo myIns = (CFoo)....

Juha Ka
 
I don't know if there is any way of doing the equivalent of "Class.forName()". But you can implement a factory class that creates an object based on the string you pass as parameter.
You could do this also in the base class, in a static method, something like:

public static MyBaseClass CreateObject(String objectType)
{
if(objectType == "DerivedClass1)
return new DerivedClass1();
....
}
 
I don't know if there is any way of doing the equivalent of "Class.forName()". But you can implement a factory class that creates an object based on the string you pass as parameter.
You could do this also in the base class, in a static method, something like:

public static MyBaseClass CreateObject(String objectType)
{
if(objectType == "DerivedClass1")
return new DerivedClass1();
....
}
 
My idea is to create a dynamically extendable Windows Service. I'm finding out the way to add a new dll (=new class) without recompile and reinstall the service. Modifying the settings file and restarting the service should be enough.....

Juha Ka
 
You can use reflection to do that, provided that you have a common interface for the extensions of the service.
 
Thanks to all, now I know two ways to do that dynamically! But using reflection is easier way, I think.

(Add two namespaces:
using System.Reflection;
using System.Runtime.Remoting;

and, of course, the namespace of CFoo...
....
(in the code)
ObjectHandle objHand = Activator.CreateInstance("DLL-name", "namespace.MyClass3");
CFoo inst = (CFoo)ojbHand.UnWrap()
)

That's all, now I can dynamically choose which instance I create by changing the parameters of Activator.CreateInstance() -method.

Juha Ka

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top