J
Jer
Hi all,
I am trying to get plugins working in my application.
I am using the approach of writing an interface and then implementing that
interface in each plugin. I would like to be able to just have a plugin
directory, and on program launch scan that directory, load each dll in it
and decide whether it's a valid plugin dll.
So far I've gotten the directory scan fine, loaded each assembly in turn,
but when I try to cast an object that implemements the interface I get an
invalid cast exception. Any ideas?
Below are two different attempts at this... comments would be greatly
appreciated as I'm thinking I don't know how to read documentation at this
point.
**** Attempt 1 *****
foreach (string file in pluginDlls)
{
try
{
System.Reflection.Assembly pluginAssembly = Assembly.LoadFrom(file);
Type[] typesInAssembly = pluginAssembly.GetTypes();
foreach (Type type in typesInAssembly)
{
if (type is Interfaces.IDBConnector)
{
// This is a valid DBConnector plugin!
// Grab information and load it.
Interfaces.IDBConnector connector =
(Interfaces.IDBConnector)pluginAssembly.CreateInstance(type.FullName);
this.availablePlugins.Add(connector);
// Do more now
**** Attempt 2 ******
System.Reflection.Assembly pluginAssembly = Assembly.LoadFrom(file);
Type[] typesInAssembly = pluginAssembly.GetTypes();
foreach (Type type in typesInAssembly)
{
object o = pluginAssembly.CreateInstance(type.FullName);
Interfaces.IDBConnector connector = null;
try
{
connector = (Interfaces.IDBConnector)o;
}
catch (InvalidCastException)
{
connector = null;
}
if (connector != null)
{
// This is a valid DBConnector plugin!
// Grab information and load it.
Now, Attempt 1 NEVER finds any types that match the line "if (type is
Interfaces.IDBConnector)" and Attempt 2 always throws an
InvalidCastException.
I look forward to hearing your ideas...
Jeremy Wiebe
jeremywiebe at mailblocks dot com
I am trying to get plugins working in my application.
I am using the approach of writing an interface and then implementing that
interface in each plugin. I would like to be able to just have a plugin
directory, and on program launch scan that directory, load each dll in it
and decide whether it's a valid plugin dll.
So far I've gotten the directory scan fine, loaded each assembly in turn,
but when I try to cast an object that implemements the interface I get an
invalid cast exception. Any ideas?
Below are two different attempts at this... comments would be greatly
appreciated as I'm thinking I don't know how to read documentation at this
point.

**** Attempt 1 *****
foreach (string file in pluginDlls)
{
try
{
System.Reflection.Assembly pluginAssembly = Assembly.LoadFrom(file);
Type[] typesInAssembly = pluginAssembly.GetTypes();
foreach (Type type in typesInAssembly)
{
if (type is Interfaces.IDBConnector)
{
// This is a valid DBConnector plugin!
// Grab information and load it.
Interfaces.IDBConnector connector =
(Interfaces.IDBConnector)pluginAssembly.CreateInstance(type.FullName);
this.availablePlugins.Add(connector);
// Do more now
**** Attempt 2 ******
System.Reflection.Assembly pluginAssembly = Assembly.LoadFrom(file);
Type[] typesInAssembly = pluginAssembly.GetTypes();
foreach (Type type in typesInAssembly)
{
object o = pluginAssembly.CreateInstance(type.FullName);
Interfaces.IDBConnector connector = null;
try
{
connector = (Interfaces.IDBConnector)o;
}
catch (InvalidCastException)
{
connector = null;
}
if (connector != null)
{
// This is a valid DBConnector plugin!
// Grab information and load it.
Now, Attempt 1 NEVER finds any types that match the line "if (type is
Interfaces.IDBConnector)" and Attempt 2 always throws an
InvalidCastException.
I look forward to hearing your ideas...
Jeremy Wiebe
jeremywiebe at mailblocks dot com