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

  • SEARCH
  • Home
  • 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 8916865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:21:26+00:00 2026-06-15T05:21:26+00:00

I’m writing a framework, one function of which involves remotely handling events (details of

  • 0

I’m writing a framework, one function of which involves remotely handling events (details of which I believe aren’t relevant right now). What is needed though is the ability to hook an event handler to any event, given the target object reference and event name.

The method that handles the event will marshal/otherwise proxy the parameters to some handler code, then cast the return type to the event handler’s return type. Effectively, it wraps the handler.

I believe that due to the fact that the event handler may be required to have any number of parameters of any type, the only way to handle a request to hook an event on-demand is to use reflection/emit (code generation) to build a custom proxy method. I’m posting this question to check that assumption and see if anyone has any other/neater ideas.

Currently, I create a DynamicMethod, then do this kind of thing. It works – almost – but the wrapper method that this dynamic proxy method calls must be static (the declaration is pretty much public static object GenericHandler(object[] parameters)); I don’t want it to be. So my current task is to dynamically create a whole type, with this method, but also a field to store the instance of the object to call the wrapper on. But, that’s beside the point (I think) – I want to check that my assumption about having to create dynamic code at all is correct or not. But, for ref:

        // obj = target object, eventName = event name
        var evtInfo = obj.GetType().GetEvent(eventName);

        var handlerType = evtInfo.EventHandlerType;

        var eventInvokeMethod = handlerType.GetMethod("Invoke");
        var paramTypes = eventInvokeMethod.GetParameters().Select(p => p.ParameterType).ToArray();
        var returnType = eventInvokeMethod.ReturnType;

        var handlerMethod = new DynamicMethod(
            "evtHandler_" + eventName,
            MethodAttributes.Static | MethodAttributes.Public,
            CallingConventions.Standard,
            returnType,
            paramTypes,
            typeof(my container class).Module,
            false);

        var il = handlerMethod.GetILGenerator();

        // locals: [0] = parameter array
        il.DeclareLocal(typeof(object[]));

        // create an appropriately-sized array objects, for the parameters
        il.Emit(OpCodes.Ldc_I4, (int)paramTypes.Count());
        il.Emit(OpCodes.Newarr, typeof(object));
        il.Emit(OpCodes.Stloc_0);

        for (int i = 0; i < paramTypes.Length; i++)
        {
            // for the array: load the parameter into the same index of the array
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Ldc_I4, i);
            il.Emit(OpCodes.Ldarg, i);
            if (!paramTypes[i].IsClass)
            {
                // box the value first..
                il.Emit(OpCodes.Box, paramTypes[i]);
            }
            il.Emit(OpCodes.Stelem_Ref);
        }

        // call our generic handler with the array of parameters, then convert the return
        // value to the target type via unboxing or a cast
        il.Emit(OpCodes.Ldloc_0);
        il.Emit(OpCodes.Call, wrapper.GetMethodInfo());
        if (!returnType.IsClass)
        {
            il.Emit(OpCodes.Unbox_Any, returnType);
        }
        else
        {
            il.Emit(OpCodes.Castclass, returnType);
        }
        il.Emit(OpCodes.Ret);

If there are no responses and I get the above theory with wrapper type working, I’ll post updated code.. but would love to hear about alternatives to this method if they are out there.

  • 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-06-15T05:21:28+00:00Added an answer on June 15, 2026 at 5:21 am

    It was necessary to implement (and improve upon) the above code. With a dynamically-generated assembly, type and method, I can proxy any events fired to a general object HandleEvent(object[] parameters) method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

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.