473,804 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9549
yes, you can

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

Type type = otherAssemby.Ge tType(typename) ;

object obj = otherAssembly.C reateInstance(t ype);

BindingFlags bf = BindingFlags.In vokeMethod | BindingFlags.In stance |
BindingFlags.Pu blic | BindingFlags.No nPublic;

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

// or
ISomeWellKnownI nterface ifc =
(ISomeWellKnown Interface)other Assembly.Create Instance(typena me);
ifc.methodname( arg1, arg2);

Jan 25 '06 #2
Something like this?

Assembly a = Assembly.LoadWi thPartialName(" MyAssembly");
object obj = a.CreateInstanc e("MyType");
Type[] signature = new Type[] {typeof(int), typeof(string)} ;
MethodInfo method = obj.GetType().G etMethod("MyMet hod",
signature);
object[] parameters = new object[] { 123, "abc" };
method.Invoke(o bj, 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.Ge tType(typename) ;

object obj = otherAssembly.C reateInstance(t ype);

BindingFlags bf = BindingFlags.In vokeMethod | BindingFlags.In stance |
BindingFlags.Pu blic | BindingFlags.No nPublic;

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

// or
ISomeWellKnownI nterface ifc =
(ISomeWellKnown Interface)other Assembly.Create Instance(typena me);
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.LoadWi thPartialName(" MyAssembly");
object obj = a.CreateInstanc e("MyType");
Type[] signature = new Type[] {typeof(int), typeof(string)} ;
MethodInfo method = obj.GetType().G etMethod("MyMet hod",
signature);
object[] parameters = new object[] { 123, "abc" };
method.Invoke(o bj, 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.Ge tType(typename) ;

object obj = otherAssembly.C reateInstance(t ype);

BindingFlags bf = BindingFlags.In vokeMethod | BindingFlags.In stance |
BindingFlags.Pu blic | BindingFlags.No nPublic;

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

// or
ISomeWellKnownI nterface ifc =
(ISomeWellKnown Interface)other Assembly.Create Instance(typena me);
ifc.methodname( arg1, arg2);

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

Jan 25 '06 #7
Hi,

"Christian Havel" <Ch************ @discussions.mi crosoft.com> wrote in
message news:A4******** *************** ***********@mic rosoft.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.LoadFr om 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.Assem blyResolve 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.mi crosoft.com> wrote in
message news:A4******** *************** ***********@mic rosoft.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.LoadFr om 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
2299
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 .... py> C().spam(3) spam spam spam
4
20306
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 handy to be able to instantiate them based on a string variable representing their class name. Here's an example of what I'd be looking to do. public object DynamicInstantiation(string className) { return new ; }
1
1749
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* to be front-ended on multiple databases as well as rendered in multiple browser types. That being said, is there a pre-constructed class out there that I can call from within my code to systematically "build" my SQL statement and have it take...
4
17028
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 can't seem to create it or call methods to this newly created instance. I have the following code: public class Script {
1
2984
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 DLL. I washoping I'd be able to use it using the DllImport attribute, andcreating empty function declarations. I've done that before andit works well. The problem is that DllImport requires that allfunctions be static. This particular DLL requires...
4
3667
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 in advance knowing how many objects (employee1, employee2, etc) Dim player1 As New Persons.Players Dim player2 As New Persons.Players Dim player3 As New Persons.Players ....
2
3121
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 dynamically loaded assembly) so far so good (per my code below)... but here is where I'm getting hung up: 3. Call methods of that type (see comments in my code) If the types in the dynamically loaded assembly were in the same namespace
3
3585
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 be instantiated. The user does not have
7
3243
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, **itemArgs) where "cls" is the class reference, and itemArgs is obviously a set of keyword arguments for its __init__ method. But now I want to generalize this to handle a set of mix-in classes.
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.