Import DLL at run time in VB.NET

i am working on VB.NET 2005 application
i am developing Plug in intrefaces..
my question is How to import a DLL at run time and how to read all the Public function or methods in the class...

Who is Participating?
RamanaChoudaryConnect With a Mentor Author Commented:
Hi paulHews,
i found answer...
this is exactly what i am looking for..

thank you for Input
Assuming you want them to derive from a common interface, you create the interface first in an assembly, and reference that assembly in both the client and plugins.  Then you can create instances using reflection.


Plugin architecture in

Private Function GetPluginFromAssembly(ByVal PluginAssembly As String, ByVal PluginClassName As String, ByVal PluginAssemblyPath As String) As PluginBase.IPlugin
    Dim oCurrentAssembly As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly()
    Dim oTemplateAssembly As [Assembly]
    Dim oPlugin As Object
    Dim sTemplateNameAndPath As String = System.IO.Path.Combine(PluginAssemblyPath, PluginAssembly)
    If System.IO.File.Exists(sTemplateNameAndPath) Then
        oTemplateAssembly = Assembly.LoadFrom(sTemplateNameAndPath)
        oPlugin = Activator.CreateInstance(oTemplateAssembly.GetType(PluginClassName, True))
        Return CType(oPlugin, PluginBase.IPlugin)
    End If 
End Function

Open in new window

RamanaChoudaryAuthor Commented:
i am sorry for my confuse question..
here what i want to do...
i have a windows application.. this application is referencing some dll's
then we calling Public functions in that dll's
so every 15 days we want create a another dll and call that function..
its  getting very head achace..
so we plan to write reffrence the dll at run time and call that function(we define Function name in APP.CONFIG File)

basically what i need i want to add a dll at runtime to my project and Get all function names then i can compare with function name in app.config file..

hope this understood
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Éric MoreauSenior .Net ConsultantCommented:
It can get complicated, but essentially you want to use Activator.CreateInstance to load the assembly, then load the classes and methods, and invoke the method

MyDLL code:
Public Class TestClass
    Public Function TestFunction(ByVal x As Integer, ByVal y As String) As String
        MsgBox(String.Format("Using {0} and '{1}'", x, y))
        Return "Hello world"
    End Function

End Class

TestInvoke code:

Imports System.Reflection
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        treNovelStructure.HideSelection = False
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim AssemblyPath As String = "C:\Path To DLL\MyDLL.dll"
        Dim A As Assembly = Assembly.LoadFrom(AssemblyPath)
        Dim Plugin As Object = A.CreateInstance("MyDLL.TestClass")
        Dim t As Type = Plugin.GetType()
        Dim mi As MethodInfo = t.GetMethod("TestFunction", BindingFlags.Public Or BindingFlags.Instance)
        Dim Ret As String = mi.Invoke(Plugin, New Object() {1, "Hi"}).ToString
    End Sub
End Class

Open in new window

RamanaChoudaryAuthor Commented:
hi PaulHews:

thanks for your code.already i have same code
but the thing is I dont want define Class name and Function name
once i load my DLL, i need to get all Classes and Functions in that Dll..
do you have any sample code for this?

I answered the question as posted.  I then answered the question you claim you meant to ask.  (You said "we define Function name in APP.CONFIG File")

The link you post for your solution doesn't make a lot of sense, because somewhere you're going to have to know what class/function you need to call in your DLL--Unless you are calling functions at random, which doesn't make much sense either.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.