Question How do import .NET libraries at runtime?

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
Hi there,

I am developing a plugin for an application which means I have to import a library into my library at runtime.

The library is a .NET dynamic link library and I need to be able to use the structures/methods it houses within my application (plugin library).

How would I go about doing this? Anyone have a link to an article/tutorial explaining this?

Thanks.
 
Last edited:
why not just do your imports up front as you would for any other namespace?
Because the assembly isn't referenced at design time, it's loaded during runtime. You don't understand the question obviously.
 
Thanks JohnH, i've had a quick look and it looks like page 3 if what i need.

why not just do your imports up front as you would for any other namespace?

Basically, because the .exe file that will run my plugin (which is a .dll file) has not referenced the library that i want to use, I have to write some logic in my plugin to load it at runtime.
 
I wrote a plugin based app and the keystone of it is an abstract class that defines the contract of a plugin.. To whit all plugins must reference my assembly and derive from MyPluginBase

I can give you the code that loads the assemblies dynamically if you like.. It uses a filesystemwatcher to notice for plugin dlls appearing during runtime, and it creates instances of them as they appear (other code does a scan of the dir on startup for existing plugins)
Cool part is, the plugins can be upgraded on the fly (design requirement) ithout stopping the service
 
I wrote a plugin based app and the keystone of it is an abstract class that defines the contract of a plugin.. To whit all plugins must reference my assembly and derive from MyPluginBase

I can give you the code that loads the assemblies dynamically if you like.. It uses a filesystemwatcher to notice for plugin dlls appearing during runtime, and it creates instances of them as they appear (other code does a scan of the dir on startup for existing plugins)
Cool part is, the plugins can be upgraded on the fly (design requirement) ithout stopping the service

If you could post an example it'd be great, but its probably far more advanced than i need at this time.
 
OK - i have read through the article many times, and have realised that I may have posted this in the wrong section.

Would you still give the same advice if i said that the library I am trying to import doesn't have an interface, I just want to use a few types, methods + callbacks in it.

I tried using the method in the link, but i can't get my head around it.

I can import the assembly fine, but I don't really have a clue how to use its methods and types etc....

Been looking at this and it tells me how to get a link to a method - but not how to run and return stuff from the method: http://msdn.microsoft.com/en-us/library/1009fa28.aspx
 
Last edited:
Not using an interface means you have to operate solely with System.Reflection (stuff like the ExamineAssembly method in article). Basically just making it harder for yourself.
 
Not using an interface means you have to operate solely with System.Reflection (stuff like the ExamineAssembly method in article). Basically just making it harder for yourself.

Well i am not the creator of the library file, so how do i create an interface too it?.....its a 3rd party library file....
 
If you design an app for plugins you must require the authors of plugin libs to implement certain interfaces you define to be compatible. That's how plugin architecture works.
 
Yeah - i'm definately posted in the wrong section....

I am creating the plugin for a 3rd party application. Because the 3rd party application does not reference the .DLL i need in my plugin, I must import it into MY plugin at runtime...

...sorry for the confusing explanation, I am not particularly good at explaining things, and it probably doesn't help that i have posted in the wrong section.

So just incase there is any confusion - i'm NOT creating an application that will use plugins - I AM creating a plugin for an existing application which has an interface.
 
In that case I don't understand why you don't reference the lib at design time. The app is not your department, only your own assembly project is. If you want your project to use other libraries you just reference them.
 
I swear, sometimes i even amaze myself with my own stupidity...

You must think im a idiot (i don't blame ya), the reason it didn't work is because i forgot to change the build to x86 only (my development PC in x64).

Thank you very much for your help!

EDIT: I don't have the edit button on my opening post so i can change the prefix?
 
EDIT: I don't have the edit button on my opening post so i can change the prefix?

We know (it's why "Resolved" marking sucks a little on VBDNF.. Neal is aware of it but unwilling to install any unofficial hacks that allow one-click resolution marking) so your question will have to remain looking like a question..


One day.. ;)
 

Latest posts

Back
Top