473,386 Members | 1,828 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,386 software developers and data experts.

Dynamic function call

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
Nov 15 '05 #1
4 8461
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Reflection is rather "expensive" method to do this, are there other ways to
implement...?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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" <ta**********@tcon-NOSPAM.co.il>
| References: <u4**************@tk2msftngp13.phx.gbl>
<MP************************@msnews.microsoft.com >
| 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: <eH*************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 198.211.173.74
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFT NGXA05.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...?
|
|
| "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
| news:MP************************@msnews.microsoft.c om...
| > Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
| > > 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 - <sk***@pobox.com>
| > http://www.pobox.com/~skeet
| > If replying to the group, please do not mail me too
|
|
|

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: prashna | last post by:
Hi all, Is'nt a function invocation through a function pointer is dynamic binding? For example consider the following program 1 int main() 2 { 3 int (*fun_ptr)(); 4 int...
3
by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
5
by: markww | last post by:
Hi, Can someone explain to me what static and dynamic dispatch are and what the difference is between the two? Do this even occurr in C++? Thanks
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
0
by: imranabdulaziz | last post by:
hi all , i am mess with the one situation. i am using asp.net2.0 ,C# and sql server 2005. I have checkboxlist and based on user selection i creates dynamic controls(which code is in...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.