Dynamic function call

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

Is it possible (as was in VB - CallByName) to call function which name was
generated.
Example:

private static void DS_function()
{
}

private static void FD_function()
{
}

public static void call(string prefix)
{
CALLBYNAME(prefix+"_function()"); //VB - like style
}

So is it possible to implement in C# ???

Thanx
 
Tamir Khason said:
Is it possible (as was in VB - CallByName) to call function which name was
generated.
Example:

private static void DS_function()
{
}

private static void FD_function()
{
}

public static void call(string prefix)
{
CALLBYNAME(prefix+"_function()"); //VB - like style
}

So is it possible to implement in C# ???

Use reflection (in particular Type.GetMethod/GetMethods) to get the
MethodInfo, and then Invoke it.
 
Reflection is rather "expensive" method to do this, are there other ways to
implement...?
 
Tamir Khason said:
Reflection is rather "expensive" method to do this, are there other ways to
implement...?

No - it's an inherently "expensive" operation, as you're binding at
runtime rather than compile-time. If you have a limited number of
calls, you could do it with a switch statement instead:

switch (prefix)
{
case "DS":
DS_function();
break;
// etc
}

To be honest though, it's not generally a very nice way of doing
things.
 
Hi Tamir,

..Net Reflection provides you ability to dynamically create and invoke types.
Reflection's design aim is for dynamic invoke or create, so it is fit for
you.
While just as Jon said, the enumerate way of solution is not a general way.

If you still have anything unclear, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Tamir Khason" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Dynamic function call
| Date: Tue, 11 Nov 2003 11:18:49 +0200
| Lines: 36
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 198.211.173.74
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:198305
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Reflection is rather "expensive" method to do this, are there other ways
to
| implement...?
|
|
| | > > Is it possible (as was in VB - CallByName) to call function which name
| was
| > > generated.
| > > Example:
| > >
| > > private static void DS_function()
| > > {
| > > }
| > >
| > > private static void FD_function()
| > > {
| > > }
| > >
| > > public static void call(string prefix)
| > > {
| > > CALLBYNAME(prefix+"_function()"); //VB - like style
| > > }
| > >
| > > So is it possible to implement in C# ???
| >
| > Use reflection (in particular Type.GetMethod/GetMethods) to get the
| > MethodInfo, and then Invoke it.
| >
| > --
| > Jon Skeet - <[email protected]>
| > http://www.pobox.com/~skeet
| > If replying to the group, please do not mail me too
|
|
|
 
Back
Top