How load a DLL dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brentonn
    New Member
    • Nov 2008
    • 1

    How load a DLL dynamically

    I am working in VB 2005. I want to be able to load a DLL selected dynamically from a list.

    I have had advice and this is where I am so far:

    *************** *************** *************** *************** **
    thisAssemblyObj ect = System.Reflecti on.Assembly.Get EntryAssembly()

    returnValue = thisAssemblyObj ect.CreateInsta nce("DeviceComm s.DeviceComms" ...)
    *************** *************** *************** *************** **

    Whatever I have tried in the second line has not worked and I am at a loss to know what to try.

    Brenton
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    firstly you need to get all the Types of the current assembly... loop through it n then create a object..have a look at this ...
    Code:
    try
                {
                    Assembly myass = Assembly.GetEntryAssembly();//GetExecutingAssembly();
    
                    Type[] t = myass.GetTypes();
    
                    foreach (Type tt in t)
                    {
                        Console.WriteLine(tt.FullName.ToString());
    
                        if (tt.FullName.ToString() == "ConsoleApplication2.Program")//class namespace+classname
                        {
                            object o = Activator.CreateInstance(tt);
    
                            if (o != null)
                            {
                                MethodInfo m = o.GetType().GetMethod("my");
    
                                m.Invoke(o, null);
                            }
                        }
                    }
    
    
                }
                catch (Exception eee)
                {
                    Console.WriteLine(eee.Message.ToString());
                }

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I have just been researching this myself.

      I happen to have a DLL that I use as a webserver, and I wish to load it dynamically in my program:
      [code=c#]
      //
      //

      Assembly asm = Assembly.LoadFr om("myHttpListe ner.dll");
      object o;//this will become the instance of my class
      //the constructor takes 1 argument, an integer that defines the port to listen for connections on
      object[] myparams = new object[]{8090};
      //create an instance of "Plater.HttpLis tener", not case sensitive, default bindingflags and myparams contains the arguments passed to constructor
      o = asm.CreateInsta nce("Plater.Htt pListener", true, BindingFlags.De fault, null, myparams, null, null);
      //Now, by using the Type, I will invoke my "Start" function in my class
      //Note that I provided the object instance of my class "o" as the object to invoke the function on
      o.GetType().Inv okeMember("Star t", myflags | BindingFlags.In vokeMethod, null, o, null);

      //
      //
      [/code]

      Comment

      Working...