473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Application with plugin architecture

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.CreateInst ance(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
Nov 15 '05 #1
4 1935
Hi Jer,

Please try replacing the line:

"if (type is Interfaces.IDBConnector)"

with the following line:

if(type is typeof(Interfaces.IDBConnector))
{
}

Hopefully that should sort out things.

Regards,
Aravind C

"Jer" <je*********@mailblocks.com> wrote in message
news:Qf8rb.344879$9l5.92063@pd7tw2no...
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.CreateInst ance(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

Nov 15 '05 #2
Jer
Thanks for the response.

Unfortunately, that doesn't appear to be valid C# syntax. I am using Visual
Studio .NET 2002 and that specific line of code results in a "Type expected"
syntax error.

This is an example of something that I have never seen a good explanation
for in any documentation I've read. That is... what is the best way to
check if an object supports a given interface?

is it: if (myObject is IMyInterface) {...}
or: if (myObject.GetType() == typeof(IMyInterface) {...}
or: if (???

Jeremy Wiebe

jeremywiebe at mailblocks dot com

"Aravind C" <ar***********@nospam.hotmail.com> wrote in message
news:u4**************@TK2MSFTNGP09.phx.gbl...
Hi Jer,

Please try replacing the line:

"if (type is Interfaces.IDBConnector)"

with the following line:

if(type is typeof(Interfaces.IDBConnector))
{
}

Hopefully that should sort out things.

Regards,
Aravind C

"Jer" <je*********@mailblocks.com> wrote in message
news:Qf8rb.344879$9l5.92063@pd7tw2no...
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.CreateInst ance(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


Nov 15 '05 #3
Hi Jeremy,
pls, try this:

foreach(Type type in typesInAssembly)
{
Type my_interface =
type.GetInterface("IDBConnector");
if (my_interface != null) //we've got it
.....

Or, if this not works, try to iterate type.GetInterfaces() to see what
exactly is stored for that class, and after that, change the string in
GetInterface("").

Please, post the results

Sunny

In article <Qf8rb.344879$9l5.92063@pd7tw2no>, je*********@mailblocks.com
says...
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.CreateInst ance(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

Nov 15 '05 #4
Forgot:

if (type is Interfaces.IDBConnector)


is not working, becouse "is" checksh the the type of first parameter,
i.e. here your variable type is Type, and Type is != IDBConnector, thats
why it shows false all the time.

Sunny
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chua Wen Ching | last post by:
Hi there, I had some doubts on creatings plugins. As most example on the internet shows how to write plugins onto a plugin host which is normally a windows form program. 1) Can i replace...
5
by: Tamir Khason | last post by:
Friends, maybe someone knows good references for .NET plugin based program architecture. E.g I want to be able to "put" class library(dll) in some place (where I do not the name of the class, but...
3
by: CSharpNewBie | last post by:
Hi I am looking to create a Plugin Architecture. I looked at this extend or enhance the User Interface (UI) article http://www.codeproject.com/csharp/extensibleui.asp and It looks good and I...
0
by: Dan Dorey | last post by:
I'm in the midst of creating a plugin framework with the goal of making it as easy as possible for myself and other developers to both create new plugins and work with existing ones. Each plugin...
6
by: S. Lorétan | last post by:
Hi guys. I am preparing the rewriting of an huge existing application in VB6. This software is a mess of 10 years of patchs and new functionalities added randomly, and didn't have any logical or...
4
by: Paciente8159 AKA Klayman | last post by:
Hi, I have a couple of doubts reggarding a plugin based application in C++? I want to build a c++ plugin based app. I have searched alot of things in the net but I still don't know how to...
4
by: anglozaxxon | last post by:
I'm making a program that consists of a main engine + plugins. Both are in Python. My question is, how do I go about importing arbitrary code and have it be able to use the engine's functions,...
22
by: Wildemar Wildenburger | last post by:
To make it short: Is there something like this already? There seem to loads of python frameworks for Web-Apps, but I have a hard time finding one for desktop-apps. I imagine it wouldn't be too...
3
by: =?UTF-8?B?R3J6ZWdvcnogU8WCb2Rrb3dpY3o=?= | last post by:
I'm working on my little project (an IM client) which I wanted to support plugins. My idea was that the core program would by itself do virtually nothing but manage plugins and all functionality...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.