Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

calling a method using it's name in C#

How can I call a method using it's name in C# ?

Here is the snippet of code:

myMethod; //myMethod, which is a string contains checkDuplicate()

I want myMethod to invoke the method, checkDuplicate().

I believe there was something like 'call stringName' in VB, but that syntax does not work in C#.

Eldo
Friday, January 16, 2004

Try looking up the MethodInfo object in the .NET Help. It's part of System.Reflection.

Dave Hallett
Friday, January 16, 2004

something like this...

MethodInfo checkDuplicateMethod = GetType().GetMethod("checkDuplicate");
checkDuplicateMethod.Invoke(this, null);

Marcus Greenwood
Tuesday, January 20, 2004

Somewhere right now, a Lisp programmer is crying.


Thursday, January 22, 2004

Without knowin the context I can't be certain, but I have a suspicion this could be a case where you'd want to use a  delegate rather than Reflection.

Reflection would be appropriate if the method name and signature cannot be known in advance (such as perhaps with a plugin system if you're not using interfaces), but if it's a case of reusing a bit of code but calling one of a limited and predefined set of methods whenever it's called, then I think 'myMethod' should be a delegate.

Think "function pointer".

Kevin Daly
Friday, January 23, 2004

You can use a delegate:

    public class SourceClass
    {
        public delegate void FunctionPointer(int arg1, string arg2);
        public FunctionPointer fp;

        public void DoSomething()
        {
            // call the function pointed to by fp;
            fp(3, this.ToString());
        }
    }

    public class SinkAClass
    {
        public void Callback(int arg1, string arg2)
        {
            Console.WriteLine("{0} Callback method was called with arguments: {1}, {2}", this.ToString(), arg1.ToString(), arg2);
        }
    }

    public class SinkBClass
    {
        public void Callback(int arg1, string arg2)
        {
            Console.WriteLine("{0} Callback method was called with arguments: {1}, {2}", this.ToString(), arg1.ToString(), arg2);
        }
    }

Then hook them up like so:

SourceClass sc = new SourceClass();
            SinkAClass sac = new SinkAClass();

            // Hook up the function and execute
            sc.fp = new SourceClass.FunctionPointer(sac.Callback);
            sc.DoSomething();

            // Hook up a new sink
            SinkBClass sbc = new SinkBClass();
            sc.fp = new SourceClass.FunctionPointer(sbc.Callback);

Phil
Wednesday, January 28, 2004

"Somewhere right now, a Lisp programmer is crying. "

I'm a javascript programmer. Is that close enough? :) Ah... good old myObject[myMethod]();

Aaron Boodman
Tuesday, February 03, 2004

namespace: Microsoft.VisualBasic
class: Interaction
method public static object CallByName(object ObjectRef, string ProcName, CallType UseCallType, params object[] Args);

Or use reflection:)

Filipovic slobodan
Wednesday, February 25, 2004

*  Recent Topics

*  Fog Creek Home