Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to import C# dll dynamically

How to import C# dll dynamically

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ansch
    wrote on last edited by
    #1

    Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused:

    P J A 3 Replies Last reply
    0
    • A Ansch

      Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused:

      P Offline
      P Offline
      Peter Stephens
      wrote on last edited by
      #2

      I personally have not tried this, but this might point you in the right direction: Try assem = System.Reflection.Assembly.Load(...) to load your assembly (C# dll). Then t = assem.GetType(...) to get your type (class, etc.) Then use t.InvokeMember(...) to call a method You can also get a ConstructorInfo object through the Type object and create an instance of your class through ConstructorInfo.Invoke(...) -- Peter Stephens

      J 1 Reply Last reply
      0
      • A Ansch

        Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused:

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        First you use Assembly myAssembly = Assembly.LoadFrom(myDllPath); passing in the location of your dll. Next you use the returned Assembly and search it for the Type you want to use (Type is the overlying type for all classes, enums, structs, delegates, and events). Type myType = myAssembly.GetType("myNamespace.myType");. Now with your type you can create an instance of it. object myInstance = Activator.CreateInstance(myType); Now comes the fun part, invoking your method! string retVal = (string) myType.InvokeMember("MyMethodName", BindingFlags.Public | BindingFlags.InvokeMethod, null, myInstance, new object { /* any parameters here, leave empty if there are none */ } ); There you have it! You've not successfully run a method and retreived the return value. To sum it up again,

        Assembly myAssembly = Assembly.LoadFrom(/* Assembly filename/path */ myDllFilename);
        Type myType = myAssembly.GetType(/* The type to load, with namespace */ myTypeName);
        object myInstance = Activator.CreateInstance(/* The Type for the type to create */ myType);
        string retVal = (string) myType.InvokeMember(
        /* Method name */ "myMethodName",
        /* BindingFlags, tells what to look for */ BindingFlags.Public | BindingFlags.InvokeMethod,
        /* The binder to use, null to use the defaul */ null,
        /* The instance of the type to execute the method on */ myInstance,
        /* The parameters to the method */ new object { /* Leave empty if no parameters */ }
        );

        HTH, James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

        1 Reply Last reply
        0
        • P Peter Stephens

          I personally have not tried this, but this might point you in the right direction: Try assem = System.Reflection.Assembly.Load(...) to load your assembly (C# dll). Then t = assem.GetType(...) to get your type (class, etc.) Then use t.InvokeMember(...) to call a method You can also get a ConstructorInfo object through the Type object and create an instance of your class through ConstructorInfo.Invoke(...) -- Peter Stephens

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          *sigh* I should have reloaded the forum before I started typing my post :-P James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

          P 1 Reply Last reply
          0
          • J James T Johnson

            *sigh* I should have reloaded the forum before I started typing my post :-P James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

            P Offline
            P Offline
            Peter Stephens
            wrote on last edited by
            #5

            Yes, but yours has better examples :-D -- Peter Stephens

            1 Reply Last reply
            0
            • A Ansch

              Hi, I've created a C# dll using the Class library project and have a method in there that just returns a string. I'd like to import this dll in my code dynamically instead of doing the "Add reference". How would I do that? Any help would be greatly appreciated.:confused:

              A Offline
              A Offline
              Ansch
              wrote on last edited by
              #6

              Thanks guys. Great help ;)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups