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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Loading a class from DLL at run-time

Loading a class from DLL at run-time

Scheduled Pinned Locked Moved C#
csharp
3 Posts 2 Posters 1 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.
  • H Offline
    H Offline
    hhrafn
    wrote on last edited by
    #1

    Hi, I'm trying to build a kind of a plugin-factory and I'd like to load these 'plug-ins' at run-time all using the same interface ect.. What I'm missing is a way to load the Class from a dll file into my program at run-time. Using .NET 2.0 would be preffered but I'm kinda easy as long as I get a viable solution :-D thanks, HHG

    S 1 Reply Last reply
    0
    • H hhrafn

      Hi, I'm trying to build a kind of a plugin-factory and I'd like to load these 'plug-ins' at run-time all using the same interface ect.. What I'm missing is a way to load the Class from a dll file into my program at run-time. Using .NET 2.0 would be preffered but I'm kinda easy as long as I get a viable solution :-D thanks, HHG

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Looking for something like this:

      // Get paths of all available libraries in Plugin directory.
      string[] filePaths = Directory.GetFiles(FormMain.PluginsDirectoryPath, "*.dll");

      for (int i = 0; i < filePaths.Length; i++)
      {
      try
      {
      // Load the assembly and get the defined types.
      Assembly assembly = Assembly.LoadFrom(filePaths[i]);
      Type[] types = assembly.GetTypes();

      IPlugin plugin;
      for (int j = 0; j < types.Length; j++)
      {
        if (types\[j\].GetInterface(typeof(IPlugin).FullName) != null)
        {// The type implements IPlugin interface.
          //Create an instance of the IPlugin implementation and store it.
          plugin = (IPlugin) Activator.CreateInstance(types\[j\]);
          this.plugins.Add(plugin);
        }
      }
      

      }
      }


      www.troschuetz.de

      H 1 Reply Last reply
      0
      • S Stefan Troschuetz

        Looking for something like this:

        // Get paths of all available libraries in Plugin directory.
        string[] filePaths = Directory.GetFiles(FormMain.PluginsDirectoryPath, "*.dll");

        for (int i = 0; i < filePaths.Length; i++)
        {
        try
        {
        // Load the assembly and get the defined types.
        Assembly assembly = Assembly.LoadFrom(filePaths[i]);
        Type[] types = assembly.GetTypes();

        IPlugin plugin;
        for (int j = 0; j < types.Length; j++)
        {
          if (types\[j\].GetInterface(typeof(IPlugin).FullName) != null)
          {// The type implements IPlugin interface.
            //Create an instance of the IPlugin implementation and store it.
            plugin = (IPlugin) Activator.CreateInstance(types\[j\]);
            this.plugins.Add(plugin);
          }
        }
        

        }
        }


        www.troschuetz.de

        H Offline
        H Offline
        hhrafn
        wrote on last edited by
        #3

        Thanks alot, this seems promising :cool:

        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