By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Dynamically load an assembly and invoke a method

P: n/a
Hi,

I am trying to create a plug-in assembly without the need to register it in
the GAC. Reflection should help me out here. I got this far:

private void button1_Click(object sender, System.EventArgs e)
{
Assembly engine = null;
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = assemblyPath + @"Engine.dll";
engine = Assembly.Load(assemblyName);
Type[] types = engine.GetTypes();
foreach (Type type in types)
{
if ((type.FullName == "Engine.Responder") & (type.IsClass))
{
ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
object responder = ci.Invoke(null);
MethodInfo mi = type.GetMethod("ReplyToMessage");
object[] parameters = new object[2];
string msg = "Pipo de Clown";
string reply = null;
parameters[0] = msg;
parameters[1] = reply;
string returnValue = (string)mi.Invoke(responder, parameters);
txtMonitor.AppendText("reply = " + reply);
}
}
}

This is the loaded assembly's method I invoke:

public string ReplyToMessage(string msg, out string reply)
{
reply = "This is my reply.";
return reply;
}
I am doing something right because I do get all the types in the external
assembly and passing either 1 or 3 parameters throws an exception telling me
the number of parameters is wrong. With a parameter array of two strings
passed in it "succeeds" but the external method's code is never visited.
What is wrong?

Martin.
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
What happens when you use this instead:
string returnValue = (string)mi.Invoke(responder, BindingFlags.InvokeMethod,
null, parameters);

?

"Martin Maat" <du***@somewhere.nl> wrote in message
news:10*************@corp.supernews.com...
Hi,

I am trying to create a plug-in assembly without the need to register it in the GAC. Reflection should help me out here. I got this far:

private void button1_Click(object sender, System.EventArgs e)
{
Assembly engine = null;
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = assemblyPath + @"Engine.dll";
engine = Assembly.Load(assemblyName);
Type[] types = engine.GetTypes();
foreach (Type type in types)
{
if ((type.FullName == "Engine.Responder") & (type.IsClass))
{
ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
object responder = ci.Invoke(null);
MethodInfo mi = type.GetMethod("ReplyToMessage");
object[] parameters = new object[2];
string msg = "Pipo de Clown";
string reply = null;
parameters[0] = msg;
parameters[1] = reply;
string returnValue = (string)mi.Invoke(responder, parameters);
txtMonitor.AppendText("reply = " + reply);
}
}
}

This is the loaded assembly's method I invoke:

public string ReplyToMessage(string msg, out string reply)
{
reply = "This is my reply.";
return reply;
}
I am doing something right because I do get all the types in the external
assembly and passing either 1 or 3 parameters throws an exception telling me the number of parameters is wrong. With a parameter array of two strings
passed in it "succeeds" but the external method's code is never visited.
What is wrong?

Martin.

Nov 15 '05 #2

P: n/a
Martin,

I think you should be using Activator.CreateInstance() rather than invoking
to the constructor.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Philip Rieck" <st***@mckraken.com> wrote in message
news:ul****************@TK2MSFTNGP10.phx.gbl...
What happens when you use this instead:
string returnValue = (string)mi.Invoke(responder, BindingFlags.InvokeMethod, null, parameters);

?

"Martin Maat" <du***@somewhere.nl> wrote in message
news:10*************@corp.supernews.com...
Hi,

I am trying to create a plug-in assembly without the need to register it in
the GAC. Reflection should help me out here. I got this far:

private void button1_Click(object sender, System.EventArgs e)
{
Assembly engine = null;
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = assemblyPath + @"Engine.dll";
engine = Assembly.Load(assemblyName);
Type[] types = engine.GetTypes();
foreach (Type type in types)
{
if ((type.FullName == "Engine.Responder") & (type.IsClass))
{
ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
object responder = ci.Invoke(null);
MethodInfo mi = type.GetMethod("ReplyToMessage");
object[] parameters = new object[2];
string msg = "Pipo de Clown";
string reply = null;
parameters[0] = msg;
parameters[1] = reply;
string returnValue = (string)mi.Invoke(responder, parameters);
txtMonitor.AppendText("reply = " + reply);
}
}
}

This is the loaded assembly's method I invoke:

public string ReplyToMessage(string msg, out string reply)
{
reply = "This is my reply.";
return reply;
}
I am doing something right because I do get all the types in the external assembly and passing either 1 or 3 parameters throws an exception

telling me
the number of parameters is wrong. With a parameter array of two strings
passed in it "succeeds" but the external method's code is never visited.
What is wrong?

Martin.


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.