I am creating a plugin system and each plug-in has a class called "Connect",
which is what I created as the entry point for the plug-in. The problem is,
I need to get the Full type name of this to create a instance of it.
previously I was doing this a different name by using the same root
namespace name but different class names, but our new structure won't allow
for this, mainly because of possibly namespace naming conflicts between
plug-ins. So What I am doing is dynamically creating an instance of each
plug-in at load from the loaded assembly I created. But I can not figure out
how to instanciate the class now, because it needs to know what .NET
conciders the "root namespace" in the instanciation to instanciate the
class. For example one of my plug-in 's has the root namespace of
"TestPlugin1" and of course has the connect class in it. So I need the name
"TestPlugin1.Connect" to instanciate it as an object in memory. If I knew
how to get the full name like that I could easily do this.
Dim plugType as Type = Type.GetType("TestPlugin1.Connect")
dim o as Object Activator.CreateInstance(ty)
or even
Dim plugIn As IPlugin =
DirectCast(objAssembly.CreateInstance("TestPlugin1.Connect", False,
BindingFlags.CreateInstance, Nothing, aArgsList, Nothing, Nothing), IPlugin)
but the problem is getting that full name. I can get the connect name, thats
simple its always the same... I just need the root namespace it's running
in, which is the problem... does anyone know how I can do this? thanks!
Chris Dunaway - 29 Aug 2005 14:43 GMT
Probably the simplest way would be to define a common interface that
all plugins must implement. Then it does not matter about root
namespaces and such since you will always instantiate the interface
which would not change.
Brian Henry - 29 Aug 2005 14:59 GMT
and how would I go about doing that? because I've tried that before I think
and never got it to work right.
This is what I have now where IPlugin is the default plugin interface with
this definition..
Public Interface IPlugin
Sub OnBeginShutdown()
Sub OnStartup(ByVal Application As Object)
Sub OnConnect(ByVal application As Object, ByVal ConnectionMode As
ConnectionMode)
ReadOnly Property name() As String
ReadOnly Property GroupName() As String
ReadOnly Property Icon() As Drawing.Icon
ReadOnly Property Comments() As String
End Interface
<Description("holds information about loaded plugins")> _
Public Structure pluginInformation
Public Plugin As Object
Public Name As String
Public PluginGUID As Guid
End Structure
Dim objAssembly As [Assembly] = [Assembly].LoadFile(s)
Dim classToUse As String = String.Concat(objAssembly.GetName.Name,
".Connect")
Dim plugIn As IPlugin = DirectCast(objAssembly.CreateInstance(classToUse,
False, BindingFlags.CreateInstance, Nothing, aArgsList, Nothing, Nothing),
IPlugin)
'Dim plugIn As IPlugin = DirectCast(objAssembly, IPlugin)
If plugIn Is Nothing Then
Throw New Exception("Failed to load plugin")
Else
Dim plugGUIDAttribute As Attribute =
Attribute.GetCustomAttribute(objAssembly.GetType(classToUse),
GetType(GuidAttribute))
Dim plugGUID As New Guid(CType(plugGUIDAttribute, GuidAttribute).Value)
Dim pInfo As New pluginInformation
With pInfo
.Plugin = plugIn
.Name = DirectCast(plugIn, IPlugin).name
.PluginGUID = plugGUID
End With
ht_loadedPlugins.Add(pInfo.PluginGUID, pInfo)
Debug.WriteLine(String.Format("Loaded Plugin {0}, with GUID {1}",
pInfo.Name, pInfo.PluginGUID))
plugIn.OnStartup(AppObj)
End If
Herfried K. Wagner [MVP] - 29 Aug 2005 19:28 GMT
"Chris Dunaway" <dunawayc@gmail.com> schrieb:
> Probably the simplest way would be to define a common interface that
> all plugins must implement. Then it does not matter about root
> namespaces and such since you will always instantiate the interface
> which would not change.
Sample:
<URL:http://dotnet.mvps.org/dotnet/samples/codingtechnique/PlugIns.zip>

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Mattias Sjögren - 29 Aug 2005 17:32 GMT
>I am creating a plugin system and each plug-in has a class called "Connect",
>which is what I created as the entry point for the plug-in. The problem is,
>I need to get the Full type name of this to create a instance of it.
You could add an assembly-level attribute that holds the full name of
the plugin class.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.