Import DLL at run time in VB.NET

Hi
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...

LVL 4
RamanaChoudaryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulHewsCommented:
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.

Interfaces
http://www.developer.com/lang/other/article.php/939411

Plugin architecture in vb.net
http://www.developerfusion.co.uk/show/4371/

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

0
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
0
Éric MoreauSenior .Net ConsultantCommented:
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

PaulHewsCommented:
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
        MsgBox(Ret)
 
    End Sub
End Class

Open in new window

0
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?
0
RamanaChoudaryAuthor Commented:
Hi paulHews,
i found answer...
this is exactly what i am looking for..

http://www.vbdotnetheaven.com/UploadFile/rahul4_saxena/Reflection09122007031247AM/Reflection.aspx

thank you for Input
thanks
Ramana
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulHewsCommented:
Ramana,

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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.