473,238 Members | 1,915 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,238 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 9529
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
4
by: DotNetJunkies User | last post by:
Hi, Does anyone know how/if you can instantiate a C# reference type object dynamically? More specifically, my project has a number of classes that I've created and in some cases it would be very...
1
by: Me, Myself, and I | last post by:
First off, i apologize if my terminology is off... I am currently in a project that is basically a front-end to a database. In coding this, I am taking into account that it has the *potential*...
4
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
1
by: Jason Lopez via .NET 247 | last post by:
I'm having a lot of trouble trying to instantiate a C++ classfrom a DLL in a C# application. The DLL was written in C++(Visual Studio 6.0). I have the full source code, as well as the compiled...
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
7
by: Joe Strout | last post by:
I have a function that takes a reference to a class, and then instantiates that class (and then does several other things with the new instance). This is easy enough: item = cls(self,...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.