Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Dynamic instead of reflection to call method by name

Using .NET-4.0, how would I use Dynamic to accomplish the following without using reflection?

     public void InvokeMethod(string methodName)
    {
        Type t = typeof(GCS_WebService);
        GCS_WebService reflectOb = new GCS_WebService();
        MethodInfo m = t.GetMethod(methodName);
        m.Invoke(reflectOb, null);
    }
like image 576
Bleeped Avatar asked Mar 18 '11 21:03

Bleeped


People also ask

How do you call a method dynamically in Java?

Call multiple dynamic methods on objects with the invokeMethods method. To retrieve a list of supported dynamic methods for a control, use the getDynamicMethodList method. Retrieve dynamic properties with the getProperty method and set dynamic properties with the setProperty method.

What is the difference between reflection and dynamic in C#?

Reflection can invoke both public and private members of an object while dynamic can only invoke public members. dynamic is instance specific: you don't have access to static members; you have to use Reflection in those scenarios.


1 Answers

Dynamic typing in C# doesn't provide for that - the names of the members you want to access still has to be known at compile-time. (You could create the call site yourself of course and use the rest of the machinery of the DLR to resolve things, but it wouldn't be any simpler than using reflection, and it wouldn't really be using the language features.)

like image 180
Jon Skeet Avatar answered Oct 12 '22 05:10

Jon Skeet