7

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);
    }

2 Answers 2

7

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

It wouldn't be simpler, but it could be faster (assuming it can take advantage of the call site caching).
Thanks! I seen other threads here mentioning it could be done, but without examples. Now I know why there were no examples.
5

The open source framework Impromptu-Interface has methods the automate all the plumbing to use the DLR to resolve really late like this. It runs 70% faster than reflection with void returning methods.

  public void InvokeMethod(string methodName)
    {
        var reflectOb = new GCS_WebService();
        Impromptu.InvokeMemberAction(reflectOb, methodName)
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.