Hi,
I am working on a program that has a kind of plugin architecture, and I am
having a problem that I hope someone here can help me with.
The main application on this project searches through a directory for
assemblies (its plugins). A given assembly may have a configuration settings
class (which is marked a custom attribute so it is recognizable as
configuration settings class).
The main application then needs to be able to create an instance of that
config class, then load up a form which has a PropertyGrid object on it, and
set the instance of the config class to the PropertyGrid's SelectedObject
property.
I can get most of this to work, however, the property grid always seems to
be disabled or read-only when the SelectedObject is set to a plugin's Config
Settings class. The properties of the Config Class show up, but I cannot
manipulate them
I use code similar to this:
Private Sub ShowConfigForm(ByVal PlugIn As cmPlugin, ByVal
PluginConfigSettings As cmPluginConfigSettings)
'' the full path to the assembly
Dim pluginAssemblyPath As String =
My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.CombinePath(PlugIn.location, PlugIn.name), PluginConfigSettings.assemblyName)
'' load the assembly via reflection
Dim configSettingsAssembly As Assembly =
Assembly.LoadFile(pluginAssemblyPath)
Dim configSettingsObj As SettingsManager.ConfigSettingsBase
' find the config class
For Each curType As Type In configSettingsAssembly.GetExportedTypes
If curType.IsClass Then
If curType.BaseType.Name =
GetType(SettingsManager.ConfigSettingsBase).Name Then
' create an instance of the config class
configSettingsObj = Activator.CreateInstance(curType)
Exit For
End If
End If
Next
If Not IsNothing(configSettingsObj) Then
'' pass the newly created instance of the config class to the
config form
Dim config As New configForm(configSettingsObj)
config.ShowDialog(Me)
End If
End Sub
As an experiment I changed:
Dim configSettingsAssembly As Assembly =
Assembly.LoadFile(pluginAssemblyPath)
to:
Dim configSettingsAssembly As Assembly = Assembly.GetExecutingAssembly
so that the main application's configuration class would be used instead of
the the plugin's. When I did this, the PropertyGrid loaded up just fine -
fully enabled.
Any help is greatly appreciated,
Doug
Doug Salomon - 18 Jul 2007 15:42 GMT
Problem solved.
It turns out that this had nothing really to do with reflection,
createinstance or the propery grid.
The configuration class that I was loading had only one public property.
Originally, I had not given it a type, so it defaulted to Object, which the
property grid can't deal with (how could it?).
Although I had already caught that error and changed the property to a
string, and recompiled the assembly, I had not copied the corrected assembly
into my application's plugin directory. Now that I have, everything seems to
be working properly.
I need to remember that my own stupidity is, often, a more difficult
obstacle to overcome than even advanced topics like reflection. :)
For what its worth, I discovered my error, by loading up the copy of the
assembly in the plugins directory into Lutz Roeder's Reflector.
I noticed that it it was telling me that the propery was of type Object
instead of String. Initially, I thought it was some kind of defiency in
Reflector. Then I remembered that I'm stupid, and everything started falling
into place.
Reflector is great!
Doug