The basic steps are the same, don't worry so much about how it is coded in my example:
1.) Create an interface.
2.) Implement that interface in your plugin (or your form in your case)
3.) In your main app, loop through all DLLs in a specific folder and if they implement your interface, add them to a list for use later.
4.) When you are ready, call the plugins method that is implemented in the interface.
Here is a quick example I threw together:
For the interface:
PROJECT1 - The other two projects need a reference to this project.
In my class that implements the interface:Code:Public Interface TestInterface Sub DoWork(ByVal f As Form) End Interface
PROJECT2
PROJECT3Code:Imports InterfaceTesting Imports System.Windows.Forms Public Class Class1 Implements TestInterface Public Sub DoWork(ByVal f As Form) Implements InterfaceTesting.TestInterface.DoWork Dim myForm As New Form1 myForm.MdiParent = f myForm.Show() End Sub End Class
In my program that loads the plugins:
Code:Imports System.Reflection Imports InterfaceTesting Public Class Form1 Dim Plugs As List(Of TestInterface) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each p As TestInterface In Plugs p.DoWork(Me) Next End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.IsMdiContainer = True Plugs = New List(Of TestInterface) Dim files() As String = IO.Directory.GetFiles(Application.StartupPath & "\Plugins", "*.dll") Dim assm As Assembly For Each f As String In files assm = Assembly.LoadFile(f) For Each t As Type In assm.GetTypes() If Not t.GetInterface("TestInterface") Is Nothing Then Dim plugin As TestInterface = DirectCast(Activator.CreateInstance(t), TestInterface) Plugs.Add(plugin) End If Next Next End Sub End Class




Reply With Quote