Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does C# support CallByName?

Status
Not open for further replies.

xcduan

Programmer
May 21, 2002
8
0
0
US
Hi there,

VB supports CallByName. Does C# support that? How?

Please help me out, or I have to write a bunch of ugly switch--case sentences.

Thanks.

Neo
 
You can do an Assembly.Load (in System.Reflection). There are about 6 or 7 overloads available, some only need the assembly name, others can load from a COFF image.

Chip H.
 
Thanks a lot:)

What if I don't want to use assembly? I mean if the caller is in the same program as the function to be called.

For example, there is a function called F1() in the same class as the function Main(). What I want do is to let Main() get a function name ("F1") from the standard input and invoke the function.

Any suggestion?
 
In that case you'd use the CreateInstance method. Be aware that the type name is case-sensitive.

Chip H.
 
Bingo!

Thanks, man.

I would like to attach my code here to share with other C# dudes.

//Assume we have a function Test() to call by name.
string FunctionName="Test";
Type t=this.GetType();
MethodInfo mi=t.GetMethod(FunctionName);
if (mi!=null)
mi.Invoke(this, null);
else
MessageBox.Show("Wrong Function Name");
 
xcduan -

Thanks for posting your find.

If you get time, could you write it up as a FAQ? It sounds like the kind of thing that other people in the future will be asking.

Chip H.
 
Thanks for your advice.

It's done. Sorry I posted it twice by mistake:)
 
Thanks!

Your example looks very good.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top