getting GUID attribute from assembly

B

Brian Henry

I need to mark assemblies with a unique id (GUID in this case) and retrieve
it when the assembly is loaded into my application (a plug-in loader)... the
plug-in assembly all have a class in it which implements our plug-in base
class (which really is a default implementation of a interface IPlugin we
created)

The plug-in class looks like this

Imports bdb

Imports System.Runtime.InteropServices

<Guid("DD3196A0-60E2-4767-B1FE-D41B26B66A90")> _

Public Class SystemAdministration

Inherits bdb.pluginBaseClass

Private f_parentForm As bdb.frmMainWindow

Public Overrides ReadOnly Property name() As String

Get

Return "System Administration"

End Get

End Property

Public Sub New(ByVal parentForm As bdb.frmMainWindow, ByRef application As
Object)

f_parentForm = parentForm

End Sub

Public Overrides Sub StartPlugin()

Dim frmAdmin As New frmAdministration

frmAdmin.MdiParent = f_parentForm

frmAdmin.Show()

End Sub

End Class

=================================

then we have a loader function in our main application that does this, the
problem is at the line "Dim plugGUID As New Guid(CType(o_PluginInstance,
System.Runtime.InteropServices.GuidAttribute).Value)" where the cast is
invalid... how can i get the GUID off the assembly I loaded?! thanks!



====== code here for plug-in loader ==============



' s is a file listing of plug-ins in the directory the loader application
determined were good plug-ins

For Each s In al_goodPlugins

' start to load the plug-in

RaiseEvent PluginLoadStarted("Loading plug-in " & IO.Path.GetFileName(s) &
"...")

Try

' check to see if the dll's comments are saying it's a plugin

' load the file

Dim objAssembly As [Assembly] = [Assembly].LoadFile(s)

Dim classToUser As String = "BDBSDK." & Path.GetFileNameWithoutExtension(s)

' cast the assembly object into the IPlugin interface type

Dim o_PluginInstance As Object = objAssembly.CreateInstance(classToUser,
False, BindingFlags.CreateInstance, Nothing, aArgsList, Nothing, Nothing)

Dim plugGUID As New Guid(CType(o_PluginInstance,
System.Runtime.InteropServices.GuidAttribute).Value)

Dim plugIn As IPlugin = DirectCast(o_PluginInstance, IPlugin)

' if plugin is nothing then we didnt load for some reason

If plugIn Is Nothing Then

' name was plugin and it didn't load? something must of went wrong

MessageBox.Show("The plugin: " & Path.GetFileName(s) & " failed to load
properly", "Plug-in load error", MessageBoxButtons.OK,
MessageBoxIcon.Information)

Else

' if we got here the plugin load worked so far

' get name and an id for plugin plus the plugin object then add to arraylist

Dim pluginInfo As pluginInformation

pluginInfo.plugin = plugIn

pluginInfo.name = plugIn.name

pluginInfo.id = pluginCollection.Count ' since zero based index, the count
will always be the next id number of the array list

pluginCollection.Add(pluginInfo) ' add structure with plugin and info to the
plugin arraylist

End If

Catch ex As Exception

bdb.errorhandler.ErrorCaught(ex) ' we shouldn't get here, but if the plugin
is corrupt it will notify the user of it

End Try

Next
 
B

Brian Henry

I think I might of figured it out by changing a few lines to this

Dim o_PluginInstance As Object = objAssembly.CreateInstance(classToUser,
False, BindingFlags.CreateInstance, Nothing, aArgsList, Nothing, Nothing)

Dim plugGUID As Attribute =
Attribute.GetCustomAttribute(objAssembly.GetType(classToUser),
GetType(GuidAttribute))

Debug.WriteLine(String.Format("Loaded Plug-in GUID: {0}", CType(plugGUID,
GuidAttribute).Value))

Dim plugIn As IPlugin = DirectCast(o_PluginInstance, IPlugin)





Brian Henry said:
I need to mark assemblies with a unique id (GUID in this case) and retrieve
it when the assembly is loaded into my application (a plug-in loader)...
the plug-in assembly all have a class in it which implements our plug-in
base class (which really is a default implementation of a interface IPlugin
we created)

The plug-in class looks like this

Imports bdb

Imports System.Runtime.InteropServices

<Guid("DD3196A0-60E2-4767-B1FE-D41B26B66A90")> _

Public Class SystemAdministration

Inherits bdb.pluginBaseClass

Private f_parentForm As bdb.frmMainWindow

Public Overrides ReadOnly Property name() As String

Get

Return "System Administration"

End Get

End Property

Public Sub New(ByVal parentForm As bdb.frmMainWindow, ByRef application As
Object)

f_parentForm = parentForm

End Sub

Public Overrides Sub StartPlugin()

Dim frmAdmin As New frmAdministration

frmAdmin.MdiParent = f_parentForm

frmAdmin.Show()

End Sub

End Class

=================================

then we have a loader function in our main application that does this, the
problem is at the line "Dim plugGUID As New Guid(CType(o_PluginInstance,
System.Runtime.InteropServices.GuidAttribute).Value)" where the cast is
invalid... how can i get the GUID off the assembly I loaded?! thanks!



====== code here for plug-in loader ==============



' s is a file listing of plug-ins in the directory the loader application
determined were good plug-ins

For Each s In al_goodPlugins

' start to load the plug-in

RaiseEvent PluginLoadStarted("Loading plug-in " & IO.Path.GetFileName(s) &
"...")

Try

' check to see if the dll's comments are saying it's a plugin

' load the file

Dim objAssembly As [Assembly] = [Assembly].LoadFile(s)

Dim classToUser As String = "BDBSDK." &
Path.GetFileNameWithoutExtension(s)

' cast the assembly object into the IPlugin interface type

Dim o_PluginInstance As Object = objAssembly.CreateInstance(classToUser,
False, BindingFlags.CreateInstance, Nothing, aArgsList, Nothing, Nothing)

Dim plugGUID As New Guid(CType(o_PluginInstance,
System.Runtime.InteropServices.GuidAttribute).Value)

Dim plugIn As IPlugin = DirectCast(o_PluginInstance, IPlugin)

' if plugin is nothing then we didnt load for some reason

If plugIn Is Nothing Then

' name was plugin and it didn't load? something must of went wrong

MessageBox.Show("The plugin: " & Path.GetFileName(s) & " failed to load
properly", "Plug-in load error", MessageBoxButtons.OK,
MessageBoxIcon.Information)

Else

' if we got here the plugin load worked so far

' get name and an id for plugin plus the plugin object then add to
arraylist

Dim pluginInfo As pluginInformation

pluginInfo.plugin = plugIn

pluginInfo.name = plugIn.name

pluginInfo.id = pluginCollection.Count ' since zero based index, the count
will always be the next id number of the array list

pluginCollection.Add(pluginInfo) ' add structure with plugin and info to
the plugin arraylist

End If

Catch ex As Exception

bdb.errorhandler.ErrorCaught(ex) ' we shouldn't get here, but if the
plugin is corrupt it will notify the user of it

End Try

Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top