
Please Help - Need Plugin Advice!!
Quote:
> Hi,
> I am just getting into programming and vb.net and was hoping someone
> could point me into the right direction. I want to be able to create a
> number of plugins that all share the same interface and I want my
> program to scan a folder for files and then activate any that exist.
> But I don't want any preregistration of these plugins with the system
> first, just drag and drop into folder and then restart the
> application.
> Is there some technology out there that already does this? If not can
> someone please give me any hints on how to start?
> Thanks in advance,
> AJB
Here is code for loading plug-in type .dll that works for me. May not be
the best solution, but is fairly easy. It uses reflection to query a
.dll and see if it implements a certain interface:
This function loads the first class it finds in the .dll that implements
the interface. If there are more than one then you will have to alter it
as appropriate. Also, the exception handling is very generic. It could
probably use some tweaking.
Pass the path\filename of the .dll to this function and it will return
and instance of the plugin (interface)
'loads the first class that implements the interface
Public Function LoadFromAssembly(ByVal AssyName As String) As IPlugin
Dim aTypes() As Type
Dim aIntf As Type
Dim oAss As [Assembly]
Try
oAss = [Assembly].LoadFrom(AssyName)
aTypes = oAss.GetTypes()
For Each aIntf In aTypes
'If this class implements the interface
If Not (aIntf.GetInterface("PlugLib.IPlugin") Is Nothing) Then
'Return an instance of the class
Return CType(oAss.CreateInstance(aIntf.FullName),
PlugLib.IPlugin)
Exit For
End If
Next
Catch e As Exception
MsgBox("Error loading plugin [" & Path.GetFileName(AssyName) & "]:
" & e.Message)
Return Nothing
End Try
End Function
This may not be exactly what you need, but it should give you some ideas.
Chris
--
If you don't like lunchmeat, please remove it from my e-mail address to
send me an e-mail