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 8403437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:20:42+00:00 2026-06-09T22:20:42+00:00

I would like to declare common handler for variety of events, that will basically

  • 0

I would like to declare common handler for variety of events, that will basically route events to the JavaScript part of the app. I myself come for JS/AS3 background, where something like this is a relatively simple thing to do. SilverLight and C# look quite stricter though.

The plan is like follows, JS will call a special [ScriptableMember] with a class name to instantiate and method to call, as string arguments. [ScriptableMember] then must contruct an instance of the class and optionally subscribe to the events, that can be fired by that instance. Events to subscribe to, will be exposed in public static struct of some kind. [ScriptableMember] will have to extract event properties from that struct and subscribe with the single event handler to all of them. Common single event handler then should translate C# events to JS ones.

Obviously everything should be generalized and automated in some kind of factory logic.

One big problem to this is that I cannot know beforehand what will be the type of delegate or EventArgs object. And even if I knew it would have been troublesome to create an overload for each and every case. Is there anyway to define a generic event handler in C#?

UPDATE:

Here is a code snippet that deals with described logic:

[ScriptableMember]
public void exec(string compName, string action, ScriptObject scriptObject)
{
    String compFQName;
    Type compClass; 
    object comp;    

    compFQName = "fully.qualified.name." + compName;

    compClass = Type.GetType(compFQName);

    if (compClass != null) {
        comp = Activator.CreateInstance(compClass);

        FieldInfo fieldInfo = compClass.GetField("dispatches", BindingFlags.Static | BindingFlags.Public);
        object[] dispatches = (object[])fieldInfo.GetValue(comp);

        if (dispatches != null) {
            foreach (string eventName in dispatches) {
                EventInfo eventInfo = compClass.GetEvent(eventName);
                Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, "OnComponentEvent");
                eventInfo.AddEventHandler(comp, handler);
            }                       
        }
    }

    // ...
}

And then there is an event handler that should catch up all the events:

public void OnComponentEvent(object sender, EventArgs args)
{
     // do something ...
}
  • 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-09T22:20:43+00:00Added an answer on June 9, 2026 at 10:20 pm

    Every event args must be inherited from EventArgs class. If you follow this rule, you just can declare your handler like

    public void GenericHandler(object sender, EventArgs e)
    

    and subscribe it to any event

    EDIT:
    Like said O. R. Mapper, if the signature of your handler is incomparable with told above, you can call it from anonimous method that subscribed to your event, for example:

    public delegate void StupidEventHandler(int walueThatMustBeWrappedToEventArgs);
    public event StupidEventHandler StupidEventOccured;
    
    //....
    
    {
        //....
        stupidClass.StupidEventOccured += delegate(int value) { handlerObject.GenericHandler(stupidClass, new StupidEventHandlerWrapper(value)); }
    }
    

    EDIT:
    Since the question suggests unusial .NET feature usage, handling would be unusual too. Since YOU subscribe this to everything you must know what events types can occure. Then handle it like this:

    public void GenericHandler(object sender, EventArgs e)
    {
        if (e is MouseEventArgs)
        {
            var mouseArgs = e as MouseEventArgs;
            // .. process this case
        }
        else if (e is ....)
    }
    

    EDIT:

    To subscribe it try this:

    [ScriptableMember]
    public void exec(string compName, string action, ScriptObject scriptObject)
    {
        String compFQName;
        Type compClass; 
        object comp;    
    
        compFQName = "fully.qualified.name." + compName;
    
        compClass = Type.GetType(compFQName);
    
        if (compClass != null) {
            comp = Activator.CreateInstance(compClass);
    
            FieldInfo fieldInfo = compClass.GetField("dispatches", BindingFlags.Static | BindingFlags.Public);
            object[] dispatches = (object[])fieldInfo.GetValue(comp);
    
            if (dispatches != null) {
                foreach (string eventName in dispatches) {
                    EventInfo eventInfo = compClass.GetEvent(eventName);
    
                    EventHandler handlerMethod = OnComponentEvent;
                    var handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, handlerMethod.Target, handlerMethod.Method);
                    eventInfo.AddEventHandler(comp, handler);
                }                       
            }
        }
    
        // ...
    }
    

    Why your version doesn`t work is a great mistery.

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

Sidebar

Related Questions

I would like to know how do I declare a record, that has some
I would like to declare some integer constants in PowerShell. Is there any good
i would like to do this: DECLARE @TmpTable TABLE = select * from someTable
I would declare an empty String variable like this: string myString = string.Empty; Is
Would like a for loop in jquery so that: For every hover_link: show hidden
I would like to declare some user-defined compiler-constants to keep my specification files as
I would like to declare a dropdown box in a view in an ASP.NET
I would like to declare a variable 'XMLOutput' and have it produce the contents
I would like to declare some data in my Silverlight for Windows Phone 7
I would like to set up a dedicated package for all common declarations to

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.