Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6620089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:03:18+00:00 2026-05-25T21:03:18+00:00

So the obvious way to do this is.. var handler = GetType().GetMethod(methodName, BindingFlags.NonPublic |

  • 0

So the obvious way to do this is..

var handler = GetType().GetMethod(methodName, BindingFlags.NonPublic |
                                              BindingFlags.Instance);

handler.Invoke(this, new object[] {e});

And I can add caching to keep the methods around but I’m wondering if there is a completely different and faster way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:03:18+00:00Added an answer on May 25, 2026 at 9:03 pm

    The fastest way would be to cache a typed delegate; if you know the signature is always:

    void PersonInstance.MethodName(string s);
    

    Then you could create an Action<Person,string> via Delegate.CreateDelegate:

    var action = (Action<Person,string>)Delegate.CreateDelegate(
        typeof(Action<Person,string>), method);
    

    This can then be cached against the name, and invoked as:

    action(personInstance, value);
    

    Note that the cache here is critical; the reflection to locate the method and prepare a typed-delegate is non-trivial.

    It gets harder if the signature is unpredictable, as DynamicInvoke is relatively slow. The fastest way would be to use DynamicMethod and ILGenerator to write a shim method (on the fly) that takes an object[] for the parameters and unpacks and casts it to match the signature – then you can store an Action<object, object[]> or Func<object,object[],object>. That is, however, an advanced topic. I could provide an example if really needed. Essentially writing (at runtime):

    void DummyMethod(object target, object[] args) {
        ((Person)target).MethodName((int)args[0],(string)args[1]);
    }
    

    Here’s an example of doing that (note: it doesn’t handle ref/out args at the moment, and possibly a few other scenarios – and I’ve left the “cache” side of things as an exercise for the reader):

    using System;
    using System.Reflection;
    using System.Reflection.Emit;
    
    class Program
    {
        static void Main()
        {
            var method = typeof(Foo).GetMethod("Bar");
            var func = Wrap(method);
            object[] args = { 123, "abc"};
            var foo = new Foo();
            object result = func(foo, args);
        }
    
        static Func<object, object[], object> Wrap(MethodInfo method)
        {
            var dm = new DynamicMethod(method.Name, typeof(object), new Type[] {
                typeof(object), typeof(object[])
            }, method.DeclaringType, true);
            var il = dm.GetILGenerator();
    
            if (!method.IsStatic)
            {
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Unbox_Any, method.DeclaringType);
            }
            var parameters = method.GetParameters();
            for (int i = 0; i < parameters.Length; i++)
            {
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldc_I4, i);
                il.Emit(OpCodes.Ldelem_Ref);
                il.Emit(OpCodes.Unbox_Any, parameters[i].ParameterType);
            }
            il.EmitCall(method.IsStatic || method.DeclaringType.IsValueType ?
                OpCodes.Call : OpCodes.Callvirt, method, null);
            if (method.ReturnType == null || method.ReturnType == typeof(void))
            {
                il.Emit(OpCodes.Ldnull);
            }
            else if (method.ReturnType.IsValueType)
            {
                il.Emit(OpCodes.Box, method.ReturnType);
            }
            il.Emit(OpCodes.Ret);
            return (Func<object, object[], object>)dm.CreateDelegate(typeof(Func<object, object[], object>));
        }
    }
    
    public class Foo
    {
        public string Bar(int x, string y)
        {
            return x + y;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This will probably be obvious but I can't find the best way. I want
Is there an obvious way to do this that I'm missing? I'm just trying
In Xcode, there doesn't seem to be an obvious way to do this. Visual
To me, this is the obvious way to do this: given a start point
Am I thinking about this incorrectly?? Or missing something completely obvious? I can best
I'd like to have something like this work: var Events=require('events'), test=new Events.EventEmitter, scope={ prop:true
The obvious way to plot a path with virtual earth (VEMap.GetDirections) is limited to
The most obvious way to right-align a Label in WinForms doesn't work: setting anchor
I find it curious that the most obvious way to create Date objects in
I need to download several files via http in Python. The most obvious way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.