471,319 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Dynamically instantiate class and call function

Hi,

can I create a instance from a class which is another assembly and call a
function inside this assembly? I know the assembly name, class name, the
function name and its signature.

Thanks
Christian
Jan 25 '06 #1
9 9446
yes, you can

Assembly otherAssembly = Assembly.Load(...);

Type type = otherAssemby.GetType(typename);

object obj = otherAssembly.CreateInstance(type);

BindingFlags bf = BindingFlags.InvokeMethod | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic;

type.InvokeMember("methodname", bf, null, obj, new object[] {arg1,
arg2});

// or
ISomeWellKnownInterface ifc =
(ISomeWellKnownInterface)otherAssembly.CreateInsta nce(typename);
ifc.methodname(arg1, arg2);

Jan 25 '06 #2
Something like this?

Assembly a = Assembly.LoadWithPartialName("MyAssembly");
object obj = a.CreateInstance("MyType");
Type[] signature = new Type[] {typeof(int), typeof(string)};
MethodInfo method = obj.GetType().GetMethod("MyMethod",
signature);
object[] parameters = new object[] { 123, "abc" };
method.Invoke(obj, parameters);

Note: if you can, I would advise working against an interface, i.e. make
both MyAssembly and the above code reference a (single) separate assembly
with IMyInterface {void MyMethod(int value1, string value2);} - you can then
just case obj to IMyInterface and you don't need anywhere near as much
reflection.

Marc
Jan 25 '06 #3
Hi Vladimir,

thanks for your help.

Bye Christian

"Vladimir Matveev" schrieb:
yes, you can

Assembly otherAssembly = Assembly.Load(...);

Type type = otherAssemby.GetType(typename);

object obj = otherAssembly.CreateInstance(type);

BindingFlags bf = BindingFlags.InvokeMethod | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic;

type.InvokeMember("methodname", bf, null, obj, new object[] {arg1,
arg2});

// or
ISomeWellKnownInterface ifc =
(ISomeWellKnownInterface)otherAssembly.CreateInsta nce(typename);
ifc.methodname(arg1, arg2);

Jan 25 '06 #4
Hi Marc,

thanks for your help and regards from Munich.

Christian

"Marc Gravell" schrieb:
Something like this?

Assembly a = Assembly.LoadWithPartialName("MyAssembly");
object obj = a.CreateInstance("MyType");
Type[] signature = new Type[] {typeof(int), typeof(string)};
MethodInfo method = obj.GetType().GetMethod("MyMethod",
signature);
object[] parameters = new object[] { 123, "abc" };
method.Invoke(obj, parameters);

Note: if you can, I would advise working against an interface, i.e. make
both MyAssembly and the above code reference a (single) separate assembly
with IMyInterface {void MyMethod(int value1, string value2);} - you can then
just case obj to IMyInterface and you don't need anywhere near as much
reflection.

Marc

Jan 25 '06 #5
Hi,

it works! But how can I solve this problem if the assembly is located not in
the GAC but in the application directory?

Thanks
Christian

"Vladimir Matveev" schrieb:
yes, you can

Assembly otherAssembly = Assembly.Load(...);

Type type = otherAssemby.GetType(typename);

object obj = otherAssembly.CreateInstance(type);

BindingFlags bf = BindingFlags.InvokeMethod | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic;

type.InvokeMember("methodname", bf, null, obj, new object[] {arg1,
arg2});

// or
ISomeWellKnownInterface ifc =
(ISomeWellKnownInterface)otherAssembly.CreateInsta nce(typename);
ifc.methodname(arg1, arg2);

Jan 25 '06 #6
You also can subscribe to AppDomain.AssemblyResolve event and locate it
manually

Jan 25 '06 #7
Hi,

"Christian Havel" <Ch************@discussions.microsoft.com> wrote in
message news:A4**********************************@microsof t.com...
Hi,

it works! But how can I solve this problem if the assembly is located not
in
the GAC but in the application directory?


Assembly.LoadFrom will do it
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 25 '06 #8
Hi Vladimir,

thanks a lot.
Christian

"Vladimir Matveev" schrieb:
You also can subscribe to AppDomain.AssemblyResolve event and locate it
manually

Jan 25 '06 #9
Hi Ignacio,

thanks a lot and regards from Munich.

Christian

"Ignacio Machin ( .NET/ C# MVP )" schrieb:
Hi,

"Christian Havel" <Ch************@discussions.microsoft.com> wrote in
message news:A4**********************************@microsof t.com...
Hi,

it works! But how can I solve this problem if the assembly is located not
in
the GAC but in the application directory?


Assembly.LoadFrom will do it
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jan 25 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steven D'Aprano | last post: by
4 posts views Thread by DotNetJunkies User | last post: by
1 post views Thread by Me, Myself, and I | last post: by
4 posts views Thread by Ray | last post: by
1 post views Thread by Jason Lopez via .NET 247 | last post: by
reply views Thread by rosydwin | last post: by

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.