469,625 Members | 1,086 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

getting GUID attribute from assembly

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).Valu e)" 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).Valu e)

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
Nov 21 '05 #1
1 5187
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(c lassToUser),
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" <no****@nospam.com> wrote in message
news:O5**************@TK2MSFTNGP09.phx.gbl...
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).Valu e)" 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).Valu e)

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

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Razzie | last post: by
14 posts views Thread by Nak | last post: by
2 posts views Thread by Brian Henry | last post: by
9 posts views Thread by Mis Dep. | last post: by
3 posts views Thread by Jozef | last post: by
2 posts views Thread by Rob R. Ainscough | last post: by
4 posts views Thread by Ron M. Newman | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.