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

Discussion in 'C#' started by jakeriggs, Apr 23, 2008.

  1. jakeriggs

    jakeriggs New Member

    Joined:
    Apr 23, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.oceanhillsrecovery.com/
    Hi to all expert....

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

    thanks for reply....
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Create the object in a switch case statement when the right string is passed the right object is created.
     
  3. excavator

    excavator New Member

    Joined:
    Apr 25, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.hiddenbyte.com
    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
     
  4. jakeriggs

    jakeriggs New Member

    Joined:
    Apr 23, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.oceanhillsrecovery.com/
    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....
     
  5. jakeriggs

    jakeriggs New Member

    Joined:
    Apr 23, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.oceanhillsrecovery.com/
    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....
     
  6. CyCloneNL

    CyCloneNL New Member

    Joined:
    May 8, 2008
    Messages:
    7
    Likes Received:
    1
    Trophy Points:
    0
    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.
     
  7. bob smith

    bob smith New Member

    Joined:
    Jul 6, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Object ste = Class.forName("java.lang.Integer").newInstance();
     
  8. electromorph

    electromorph New Member

    Joined:
    May 18, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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?

     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice