instantiate a class based on a string being the class name??

jakeriggs's Avatar
Go4Expert Member
Hi to all expert....

How to dynamically instantiate a class based on a string being the class name???

thanks for reply....
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Create the object in a switch case statement when the right string is passed the right object is created.
0
excavator's Avatar, Join Date: Apr 2008
Newbie Member
Quote:
Originally Posted by jakeriggs
Hi to all expert....

How to dynamically instantiate a class based on a string being the class name???

thanks for reply....
you can instantiate a class dynamically based on string by using the Assembly class (System.Reflection.Assembly)

here is a simple sample code

Code:
private void LoadClass()
        {
            Assembly a = Assembly.GetExecutingAssembly() ;

            //create the class base on string
            //note : include the namespace and class name (namespace=WindowsFormsApplication1, class name=DynamicLoad)
            Object o = a.CreateInstance("WindowsFormsApplication1.DynamicLoad");

            //check to see if the class is instantiated or not
            if (o != null)
            {
                Console.WriteLine("Class Loaded");
            }
            else
            {
                Console.WriteLine("Failed");
            }          
        }
Hope this help
0
jakeriggs's Avatar
Go4Expert Member
Hi excavator....

thanks for that piece of code...Honestly, I am very new on this.....but I will just study on this code...

Thanks again....
0
jakeriggs's Avatar
Go4Expert Member
Hi excavator....

thanks for that piece of code...
Honestly, I am very new on this.....but I will just study on it...

I will just post follow up question if any.....

Thanks again....
0
CyCloneNL's Avatar, Join Date: May 2008
Light Poster
Well, although excavator's approuch will work, it isnt very 'clean'.

Why would you want to create a class, based on a string?
Where do's that string come from, user input or your own?

If its from the user, you really dont want this. They could just execute any code in your application.

If you declared that string yourself, consider changing it to an enum, or better yet think of something else.

You could for instance use Inheritance, where you have one base class and several depend on it. Thats most likely the way to go.
0
bob smith's Avatar, Join Date: Jul 2009
Newbie Member
Object ste = Class.forName("java.lang.Integer").newInstance();
0
electromorph's Avatar, Join Date: May 2010
Newbie Member
Not sure I agree it's not 'clean' - or that you don't want classes instantiated from user input. An example where I want to do this is when I'm reading a set of commands from a text file - then creating "command" classes based on the text read from each line (which are then queued & processed).

Using the "switch" approach would lead to a huge multi-choice clause which is not elegant. The CreateInstance approach removes the Switch. If I'm thinking along your lines, an enum would require a switch, wouldn't it?

Quote:
Originally Posted by CyCloneNL View Post
Well, although excavator's approuch will work, it isnt very 'clean'.

Why would you want to create a class, based on a string?
Where do's that string come from, user input or your own?

If its from the user, you really dont want this. They could just execute any code in your application.

If you declared that string yourself, consider changing it to an enum, or better yet think of something else.

You could for instance use Inheritance, where you have one base class and several depend on it. Thats most likely the way to go.