Page 1 of 1
Add-ins for your program
Posted: Mon Oct 24, 2011 11:02 am
by comathi
I was wondering how you cod imement add-ins to your program. What code should I put in the program and what code should I use in he add-in?
Please help
-Comathi-
Re: Add-ins for your program
Posted: Mon Oct 24, 2011 11:42 am
by M1z23R
Google out System.Reflection, and adding dll controls during runtime.
Re: Add-ins for your program
Posted: Mon Oct 24, 2011 12:14 pm
by PayneStudios
I have Code for that,ill post it later.
Re: Add-ins for your program
Posted: Mon Oct 24, 2011 12:49 pm
by MrAksel
You will need a DLL that both your program and the program has a reference to. Call the project PluginBase. In the code you will need a plugin class/interface that every plugin inherits/implements like:
Code: Select allPublic MustInherit Class PluginBase
Public MustOverride ReadOnly Property Name As String
Public MustOverride ReadOnly Property Creator As String
Public MustOverride ReadOnly Property Description As String
'Any optional properties you plugin might need
End Class
Or you could use an interface:
Code: Select allPublic Interface IPlugin
ReadOnly Property Name As String
ReadOnly Property Creator As String
ReadOnly Property Description As String
'Anything else
End Interface
So a plugin might have this code:
Code: Select allPublic Class MyPlugin
Inherits PluginBase.PluginBase
Public Overrides ReadOnly Property Name As String
Get
Return "PluginName"
End Get
End Property
Public Overrides ReadOnly Property Creator As String
Get
Return "PluginCreator"
End Get
End Property
Public Overrides ReadOnly Property Description As String
Get
Return "A plugin that currently does nothing."
End Get
End Property
End Class
Or the interface way:
Code: Select allPublic Class MyPlugin
Implements PluginBase.IPlugin
Public ReadOnly Property Name As String Implements PluginBase.IPlugin.Name
Get
Return "PluginName"
End Get
End Property
Public ReadOnly Property Creator As String Implements PluginBase.IPlugin.Creator
Get
Return "Creator"
End Get
End Property
Public ReadOnly Property Description As String Implements PluginBase.IPlugin.Description
Get
Return "This is a plugin that currently does nothing."
End Get
End Property
End Class
Your main project could then load the plugins:
Code: Select allPublic Function LoadPlugins(Directory As String) As PluginBase.PluginBase()
Dim PluginType As Type = GetType(PluginBase.PluginBase)
Dim Plugins As New List(Of PluginBase.PluginBase)
For Each file As String In IO.Directory.GetFiles(Directory, "*.dll")
Dim Assembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(file)
For Each [class] As Type In Assembly.GetTypes()
If [class].IsSubclassOf(PluginType) And Not [class].IsAbstract Then
Plugins.Add(Activator.CreateInstance([class]))
End If
Next
Next
Return Plugins.ToArray()
End Function
Or using the interface:
Code: Select allPublic Function LoadPlugins(Directory As String) As PluginBase.IPlugin()
Dim PluginType As Type = GetType(PluginBase.IPlugin)
Dim Plugins As New List(Of PluginBase.IPlugin)
For Each file As String In IO.Directory.GetFiles(Directory, "*.dll")
Dim Assembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(file)
For Each [class] As Type In Assembly.GetTypes()
If [class].IsSubclassOf(PluginType) And Not [class].IsAbstract Then
Plugins.Add(Activator.CreateInstance([class]))
End If
Next
Next
Return Plugins.ToArray()
End Function
I did not test this code, im sure it work, but it might be a few typos there
Re: Add-ins for your program
Posted: Mon Oct 24, 2011 8:39 pm
by comathi
Thank you so very much MrAksel. +Rep
