Wednesday, February 10, 2010

How to Load DLL without GAC without unlocking file AND without Interface

  Dim asm = Reflection.Assembly.LoadFile("d:\mydll.dll")
  Dim class1Type = asm.GetType("namespaceName.MyClassName")  'Case Sensitive 
  Dim obj = Activator.CreateInstance(class1Type)
  Dim methodInfo = class1Type.GetMethod("Exec")  'Name of the method

  Dim param() As Object = {Input, Output} 'Method input Parameters

  methodInfo.Invoke(obj, param)

for "invoking property" use class1Type.GetProperty

How to load DLL without GAC and without locking the file

The Interface (Iplugin.dll)

Public Interface IPlugin

 Sub Exec(byRef Value as string)

End Interface

The Plugin ABC.DLL (add reference to Iplugin.dll) (project name == namespace)

Public Class Plugin
Implements IPlugin.IPlugin

public sub Exec(byRef Value as string) Implements IPlugin.IPlugin.Exec

 Value = "Hello Mr" & Value

end sub

end class

The host application (Add reference to Iplugin.dll). How to use : 

Public ReadOnly Property GetPlugin(ByVal DllPath As String, ByVal ClassName As String) As IPlugin.IPlugin
Dim asm As Reflection.Assembly
asm = System.Reflection.Assembly.Load(IO.File.ReadAllBytes(DllPath))
Dim temp As Object = asm.CreateInstance(ClassName, True)
Dim obj As IPlugin.IPlugin = CType(temp, IPlugin.IPlugin)
Return obj
End Get
 End Property

dim s as string = "Nick"

Dim Obj As IPlugin.IPlugin = GetPlugin("C:\ABC.DLL", "ABC.Plugin")