Page 1 of 1

Plug-Ins How to give your applications a Plug-In ability. Rate Topic: ***** 2 Votes

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2294
  • View blog
  • Posts: 9,531
  • Joined: 29-May 08

Post icon  Posted 17 June 2009 - 11:21 AM

Create a Plug-In Based Application


Creating a Plug-In based application follows 3 basic steps
1. Defining an Interface
This where we define the contract for the plug-in.
2. Creating a Plug-In
How to create the Plug-In
3. Loading a Plug-In
How to load the Plug-In into your application.

I going to base this tutorial around a console application, to focus on the core coding needed. It can be extrapolated to a windows form application.

So lets begin.
Create a new Console application called Tutorial_PlugIns.
In the solution explorer add a new class. Called IPlugIn

1. Defining An Interface
An Interface is the contract that the plugins must follow to be a plugin.
For this tutorial we'll keep it simple, but in your application it can be as complex as it needs to be.
Change the code of IPlugIn class to this.
Public Interface IPlugIn
 ReadOnly Property Title() As String
 Sub DoThis()
 Function DoSomething(A As Double,B As Double) As Double
End Interface


Now save the application, noting where the application is save (as we'll need it later on)

2. Creating a Plugin
To Create our first Plug-In we need to add another project to our solution.

File -> Add -> Add Project -> Class Library
Name it FirstPlugIn

Next we need to add a reference to our previously created project, so we can access the IPlugIn Interface.
Add a reference to the Main Project
Underneath the class name add s Tutorial_Plugins.IPlugin, note how it adds all the required information for the plugin.
Public Class MyFirstPlugIn
 Implements Tutorial_Plugins.IPlugin


 Public Function DoSomething(ByRef A As Double, ByVal B As Double) As Double Implements Tutorial_Plugins.IPlugin.DoSomething

 End Function

 Public Sub DoThis() Implements Tutorial_Plugins.IPlugin.DoThis

 End Sub

 Public ReadOnly Property Title() As String Implements Tutorial_Plugins.IPlugin.Title
  Get

  End Get
 End Property
End Class


Now we need to give the Plug-In some functionality.

Public Class MyFirstPlugIn
 Implements Tutorial_Plugins.IPlugin


 Public Function DoSomething(ByRef A As Double, ByVal B As Double) As Double Implements Tutorial_Plugins.IPlugin.DoSomething
Return A + B
 End Function

 Public Sub DoThis() Implements Tutorial_Plugins.IPlugin.DoThis
Beep
 End Sub

 Public ReadOnly Property Title() As String Implements Tutorial_Plugins.IPlugin.Title
  Get
Return "Beep & Add Together"

  End Get
 End Property
End Class


Save the solution.
No go to Build -> Build FirstPlugIn
At this stage we have built the Plug-In, now we must create the code that utilities it.

3. Loading a Plug-In

Add a new Module to the Main Project called Plugins add the following code.

To make the loading plugin simpler I devise a subroutine to help us.
Imports System.Reflection
Namespace Plugins
 Public Module Plugins
#Const TRACE_Plugin = True
  ''' <summary>
  ''' A Generic Plugin Loader.
  ''' </summary>
  ''' <typeparam name="PlugType">The BaseClase of the Interface.</typeparam>
  ''' <param name="PluginList">A Collection into which to store the found plugins.</param>
  ''' <param name="LocationOfPlugins">The directory where the plugin DLLs are located.</param>
  ''' <param name="InterfaceName">The name of the inteface; </param>
  ''' <remarks></remarks>
  Public Sub LoadPlugins(Of PlugType)(ByRef PluginList As ICollection(Of PlugType), ByRef LocationOfPlugins As String, ByVal InterfaceName As String)
#If TRACE_Plugin Then
   Console.WriteLine("Searching For Plugins[{0}]", InterfaceName)
   Console.WriteLine("In: {0}", LocationOfPlugins)
#End If
   PluginList.Clear()
   Dim Plugin_DLLs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = FileIO.FileSystem.GetFiles(LocationOfPlugins, FileIO.SearchOption.SearchTopLevelOnly, "*.dll")
   Dim assemblyObj As Reflection.Assembly
   Dim aDescAttr As AssemblyDescriptionAttribute
   Dim aTitleAttr As AssemblyTitleAttribute
   For Each PluginDLL As String In Plugin_DLLs
	assemblyObj = Reflection.Assembly.LoadFrom(PluginDLL)
	For Each t As Type In assemblyObj.GetTypes
	 If t.IsPublic Then
	  For Each ThisParticularType As Type In t.GetInterfaces
	   Try
		If ThisParticularType.FullName = InterfaceName Then
		 PluginList.Add(CType(assemblyObj.CreateInstance(t.FullName, True), PlugType))
		 aDescAttr = CType(AssemblyDescriptionAttribute.GetCustomAttribute(assemblyObj, GetType(AssemblyDescriptionAttribute)), AssemblyDescriptionAttribute)
		 aTitleAttr = CType(AssemblyTitleAttribute.GetCustomAttribute(assemblyObj, GetType(AssemblyTitleAttribute)), AssemblyTitleAttribute)
#If TRACE_Plugin Then
		 Console.WriteLine("{0} {1}", PluginList.Count, aTitleAttr.Title)
#End If
		End If
	   Catch e As Exception
		'  Console.WriteLine(String.Format("!! Exeception: {0}", e.Message))
		' Throw e
	   End Try
	  Next
	 End If
	 ' System.Threading.Thread.Sleep(500)
	Next
   Next
#If TRACE_Plugin Then
   Console.WriteLine("Found {0} Plugins.", PluginList.Count)
#End If
  End Sub

 End Module
End Namespace


It use Generics, to make things simpler.
Plugins.LoadPlugins(Of {Name Of Interface})({Collection to contain the plugins}, {Where plugins are located}, {Full Name of Interface})

Inside the Sub Main() add the following code. (Which loads the Plug-Ins, Display it's Title, Runs DoThis, Performs the Function DoSomeThing(4,5))
Module Module1
 Sub Main()
  Dim pl As New List(Of IPlugin)
  Plugins.LoadPlugins(Of IPlugin)(pl, My.Application.Info.DirectoryPath & "\PlugIns\", "Tutorial_Plugins.IPlugin")
  For Each p As IPlugin In pl
   Console.WriteLine(p.Title)
   p.DoTHis()

   Console.WriteLine(p.DoSomething(4, 5))

  Next
  Console.ReadLine()

 End Sub

End Module


If you run the application now, you see it can't find any plugins

Tutorial_Plugins -> FirstPlugIn -> Bin ->Debug
Copy FirstPlugIn.dll to Tutorial_Plugins->Bin->Debug->PlugIns

Run Again.

Recap
1. Create an Interface
2. Create the Plug-In
3. Load & Use the Plug-In

This tutorial has only touched the basics of Plug-In but it has give you enough information so you can create you own more complex ones.


So now you'll be a Plug-In wielding coding ninja :ph34r:

Is This A Good Question/Topic? 2
  • +

Replies To: Plug-Ins

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2294
  • View blog
  • Posts: 9,531
  • Joined: 29-May 08

Posted 24 June 2009 - 12:55 PM

In 2010 using MEF (Managed Extensibility Framework)

In each of the plugins we say what were exporting, in your case its Tutorial_Plugins.IPlugin
<System.ComponentModel.Composition.Export(GetType(Tutorial_Plugins.IPlugin))>



In the main project, we state what we are importing in your case Tutorial_Plugins.IPlugin
  <Import(GetType(Tutorial_Plugins.IPlugin))>
  Dim catalog As Composition.Primitives.ComposablePartCatalog = New Composition.Hosting.DirectoryCatalog(My.Application.Info.DirectoryPath, "*.dll")
  Dim plugins As List(Of Tutorial_Plugins.IPlugin)


To load the PlugIns it now a single line of code.
   plugins = New Composition.Hosting.CompositionContainer(catalog).GetExportedObjects(Of Tutorial_Plugins.IPlugin)().ToList



Now creating Plugins are simpler.
Was This Post Helpful? 0
  • +
  • -

#3 Dapie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-August 09

Posted 08 August 2009 - 11:31 PM

It won't work and im stressing to get it too work. Please upload the files
Was This Post Helpful? 0
  • +
  • -

#4 Radjesh Klauke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-September 09

Posted 21 September 2009 - 02:04 AM

Yes, could you please upload the files?
Was This Post Helpful? 0
  • +
  • -

#5 zedth2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 121
  • Joined: 14-September 09

Posted 22 March 2011 - 04:32 PM

Thanks so much for this! It works great. Thank you!
Was This Post Helpful? 0
  • +
  • -

#6 ModestoMan75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-May 11

Posted 18 May 2011 - 12:08 AM

Hi Adam,
do you have a full example using the 2010 MEF style?
Was This Post Helpful? 0
  • +
  • -

#7 p2kpradeep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 10

Posted 01 October 2015 - 01:16 AM

I am looking for advice on how to develop plugins using VB .Net 2008, which will have their own UI and which could be loaded into and invoked from a dynamic menu in another VB .Net 2008 host application. Google search did not help, as most of them discuss how to create MS Office plugins. Could you please point me in the right direction? Many thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#8 maceysoftware  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 119
  • View blog
  • Posts: 590
  • Joined: 07-September 13

Posted 01 October 2015 - 05:50 AM

P2kpradeep this tutorial is over 6 years old with the last activity being 4 years ago.

As Adam has said in the tutorial his example is for a Console application but can be transferred to a WinForms application, have you attempted to do his example of a console application? or just gone straight to doing the WinForms/WPF i don't know what one your doing.

If you give the Console application ago and then try and do the same you may find it easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1