Plugin problem

J

JJ

Hi all,

I'm writing a program that uses plugins to add some functionality. It works
as follows:
A plugin is a user control with a method getCoordArray(decimal inX, decimal
inY, decimal inZ) and a property System.Drawing.Size bestsize. To use the
plugin, I need to do the following:
1) import the dll as a reference.
1b) validate the dll: look for the method getCoordArray(decimal inX, decimal
inY, decimal inZ) and a property System.Drawing.Size bestsize
2) adding the user control to a panel in my form (only if step 1b returns
true)
Of course I know how to do this at design time, but how can I do this at
runtime?

Thanks in advance,

Vincent
 
A

Arne Janning

JJ said:
I'm writing a program that uses plugins to add some functionality. It
works
as follows:
A plugin is a user control with a method getCoordArray(decimal inX,
decimal
inY, decimal inZ) and a property System.Drawing.Size bestsize. To use the
plugin, I need to do the following:
1) import the dll as a reference.
1b) validate the dll: look for the method getCoordArray(decimal inX,
decimal
inY, decimal inZ) and a property System.Drawing.Size bestsize
2) adding the user control to a panel in my form (only if step 1b returns
true)
Of course I know how to do this at design time, but how can I do this at
runtime?

Hi Vincent,

use System.Reflection.

//load Assembly
Assembly assm = Assembly.LoadFrom(yourDll.dll);
//get your Control
Type t = assm.GetType("yourControl");
//check if method exists
MethodInfo mi = t.GetMethod("getCoordArray");
if (mi != null)
{
ParameterInfo[] pi = mi.GetParameters();
//evaluate ParameterInfos
//[...]
//check returnType
if (mi.ReturnType == typeof (System.Drawing.Size))
{
//Create an instance of your control
Control c = (Control) assm.CreateInstance("yourControl");
//Add the control to your form
yourForm.Controls.Add(c);
//further actions with Control c
}
}

Have a look at
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconReflectionOverview.asp

for further information.

Cheers

Arne Janning
 
J

JJ

Hi,

thanks for your response. I've used your codesnippet succesfull. There's
only one problem: When I use the method getCoordArray() (that's a valid
method of my plugin, but will only be available at runtime) the compiler
gives an error message: System.Windows.Forms.Control does not contain a
definition for getCoordArray. I understand the error message, but do not
know how to solve it.

Any suggestions?

Best regards
Vincent


Arne Janning said:
JJ said:
I'm writing a program that uses plugins to add some functionality. It
works
as follows:
A plugin is a user control with a method getCoordArray(decimal inX,
decimal
inY, decimal inZ) and a property System.Drawing.Size bestsize. To use the
plugin, I need to do the following:
1) import the dll as a reference.
1b) validate the dll: look for the method getCoordArray(decimal inX,
decimal
inY, decimal inZ) and a property System.Drawing.Size bestsize
2) adding the user control to a panel in my form (only if step 1b returns
true)
Of course I know how to do this at design time, but how can I do this at
runtime?

Hi Vincent,

use System.Reflection.

//load Assembly
Assembly assm = Assembly.LoadFrom(yourDll.dll);
//get your Control
Type t = assm.GetType("yourControl");
//check if method exists
MethodInfo mi = t.GetMethod("getCoordArray");
if (mi != null)
{
ParameterInfo[] pi = mi.GetParameters();
//evaluate ParameterInfos
//[...]
//check returnType
if (mi.ReturnType == typeof (System.Drawing.Size))
{
//Create an instance of your control
Control c = (Control) assm.CreateInstance("yourControl");
//Add the control to your form
yourForm.Controls.Add(c);
//further actions with Control c
}
}

Have a look at
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconReflectionOverview
..asp

for further information.

Cheers

Arne Janning
 

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