Add-ins for your program

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Add-ins for your program
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 :D

-Comathi-
Last edited by comathi on Mon Oct 24, 2011 8:41 pm, edited 1 time in total.
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: Add-ins for your program
M1z23R
Google out System.Reflection, and adding dll controls during runtime.
User avatar
PayneStudios
Dedicated Member
Dedicated Member
Posts: 66
Joined: Thu Sep 15, 2011 2:05 pm

Re: Add-ins for your program
PayneStudios
I have Code for that,ill post it later.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Add-ins for your program
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 all
Public 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 all
Public 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 all
Public 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 all
Public 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 all
Public 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 all
Public 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
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Add-ins for your program
comathi
Thank you so very much MrAksel. +Rep :D
5 posts Page 1 of 1
Return to “Coding Help & Support”