Gunawan wrote:
I would like to create a class let's say myclass.
in myclass I have some static void function
myclass.function1()
myclass.function2() and so on.
I would like to know is there any function that I can use to call a static
function.
so I can call function1 like this
whatfunction("myclass.function1()");
whatfunction("myclass.function2()");
Some might say this is a bit perverted . . . but just for fun:
using System;
using System.Collections;
namespace WeirdFunctionCall
{
class MyClass
{
delegate void myFunc();
static Hashtable whatfunction = new Hashtable();
[STAThread]
static void Main(string[] args)
{
whatfunction.Add("myclass.function1()", new myFunc(MyClass.f1));
whatfunction.Add("myclass.function2()", new myFunc(MyClass.f2));
whatfunction.Add(" . . . and so on", new myFunc(MyClass.f3));
((myFunc)whatfunction["myclass.function1()"])();
((myFunc)whatfunction["myclass.function2()"])();
((myFunc)whatfunction[" . . . and so on"])();
Console.ReadLine();
}
static void f1() {Console.WriteLine("f1 here . . .");}
static void f2() {Console.WriteLine("f2 here . . .");}
static void f3() {Console.WriteLine("f3 here . . .");}
}
}
I have actually done things kind of like this, don't remember why it seemed like
a good idea at the time. I suppose that in VS2005 you could use a generic
Hashtable<string, myFuncand avoid the explicit cast when invoking.
HTH,
-rick-