Calling diferent Class based on String
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngescalate' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Bruno Carlos (VIP)
Hi, I asked this question on aspngfreeforall and also made a similar
question last week but so far never got a reply so I would apreciate if
anyone could help me out on this.

Bruno Carlos

[Original message clipped]

Reply to this message...
 
    
Charles M. Carroll (VIP)
Reflection. The WROX "Pro VB.net" and "Pro C#" have great reflection info.

http://www.aspfriends.com/aspfriends/ngfx-reflection.asp
is a focused place to pursue this too and there people may point you to
online Reflection Resources.

The developmentor archives linked to @
http://www.aspfriends.com/aspfriends/ngfx-reflection.asp?tab=lists
will yield great results if you search for reflection.

Answers to this should be continued on [ngfx-reflection].

At 07:48 PM 10/2/2001 +0100, you wrote:
[Original message clipped]

Reply to this message...
 
    
Andy Eunson
You can use Reflection. The code snipet below does completely dynamic code
execution. We have a database table that details the assembly name, the
class(type) name along with the method name. You may not need this level of
detail. Feel free to trim as necessary. I ripped this code out of a working
program and tried to scale it down for you. If you need more info, let me
know.

'*** These are the two primary classes that you need to call methods
dynamically.
'*** the code becomes slightly more complicated when you need to
dynamically load another assembly and call code in it. Let me know if
you;'re doing that as I can provide further sample(s).

Dim T As System.Type '*** holds information about the type
(class)
Dim Method As MethodInfo '*** holds information about the method
that you wish to call

'*** setup the parameters
Parameters have to be passed in an object array, here I'm passing
two arguments (which both are datasets which is neglible for your purposes)

Dim args(1) As Object
args(0) = Document
args(1) = RuleEvents

T = Assembly.GetType("YourClassNameHere", True)
'*** get a type descriptor

MyNewClassInstance = Activator.CreateInstance(T)
'*** Activate an actual instance of the class required

Method = T.GetMethod("YourFunctionNameHere")
'*** get the method that is to be called

Method.Invoke(MyNewClassInstance, args)
'*** invoke the method call

----- Original Message -----
From: "Bruno Carlos" <Click here to reveal e-mail address>
To: "aspngescalate" <Click here to reveal e-mail address>
Sent: Tuesday, October 02, 2001 2:48 PM
Subject: [aspngescalate] Calling diferent Class based on String

[Original message clipped]

Reply to this message...
 
    
Bruno Carlos (VIP)
But isn't there a way to do it without using Invoke?
When I call the Methods I know their identity so I wish I didn't have to
worry about sending the params in a array.

I would rather call the method in its full implementation like
Class.Method(param1, param2, etc).

Could this be done using Delegates?

Bruno Carlos

----- Original Message -----
From: "Andy Eunson" <Click here to reveal e-mail address>
To: "aspngescalate" <Click here to reveal e-mail address>
Sent: Tuesday, October 02, 2001 11:45 PM
Subject: [aspngescalate] Re: Calling diferent Class based on String

[Original message clipped]

Reply to this message...
 
    
Dan Green (VIP)
[Original message clipped]

Regardless of language you need to manually load the assembly and the type.

However, if you're using VB.NET you can use late binding to avoid the need
for Invoke calls (VB.NET produces the calls for you).

For example:

Option Strict Off
...
Dim ass as [Assembly] = [Assembly].Load(...)
Dim fooBarType as Type = ass.GetType("FooBar")
Dim fooBar as Object = Activator.CreateInstance(fooBarType)
fooBar.Method1()
fooBar.Method2(...)
...

* the square brackets are required by VB.NET because Assembly is also a
keyword.

Dan Green
[ http://dotnetdan.com -- putting the dan in .net ]

Reply to this message...
 
 
System.Activator
System.Reflection.Assembly
System.Reflection.MethodInfo
System.Type




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com