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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:35:41+00:00 2026-06-17T17:35:41+00:00

I have a generic method public delegate void Handler<T>(T val); I enable users to

  • 0

I have a generic method

public delegate void Handler<T>(T val);

I enable users to register to events and provide this delegate.
I need to save the list of delegate according to their types.

I tried saving in a dictionary of Type and object. when adding the method I cast it to a

List<Handler<T>>

according to the T. but then when an event occurred I do not have the T so cannot cast to the relevant list of generic handlers (I do have the Type but not the T)

I solved this by saving methodInfo list for each type

  private Dictionary<Type, List<MethodInfo>>  handlers = new Dictionary<Type, List<MethodInfo>>();

    public delegate void Handler<T>(T val);


    public void Register<T>( Handler<T> handler )
    {
        List<MethodInfo> lst;
        if (!handlers.TryGetValue(typeof(T), out lst))
        {
            lst = new List<MethodInfo>();
            handlers.Add(typeof(T), lst);
        }
       lst.Add(handler.Method);

    }

    public void RaiseEvent( string value)
    {
       foreach (KeyValuePair<Type, List<MethodInfo>> pair in handlers)
            {
                object typedValue;

                if (pair.Key.IsEnum)
                {
                    typedValue = Enum.Parse(pair.Key, value);
                }
                else
                {
                    typedValue = Convert.ChangeType(value, pair.Key);
                }

                foreach (MethodInfo  methodInfo  in pair.Value )
                {
                    methodInfo.Invoke(null, new[] { typedValue });
                }
            }
        }
    }

but the problem is that this approach will work only if the method is static , otherwise it will require the type of class.

is there any solution for this problem???

enable generic events…
thanks!

  • 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-17T17:35:42+00:00Added an answer on June 17, 2026 at 5:35 pm

    Maybe this will help:

    public delegate void Handler<in T>(T val);
    
    private List<Delegate> m_list = new List<Delegate>();
    
    public void AddListener<T>(Handler<T> handler) {
      m_list.Add(handler);
    }
    
    public void Call(object eventArg) {
      foreach (var handler in m_list) {
        handler.DynamicInvoke(eventArg);
      }
    }
    

    Then, if you have a handler like this:

    private void MyHandler(int val) {
      // Do something
    }
    

    You can add it to the list like this:

    AddListener<int>(MyHandler);
    

    (This assumes I correctly understood what you’re trying to do. I’m not sure though.)

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

Sidebar

Related Questions

I have an extension generic method public static void AddError<TModel>( this ModelStateDictionary modelState, Expression<Func<TModel,
I have declared a generic event handler public delegate void EventHandler(); to which I
I have forgotten the syntax for a generic method: public static void swap <T>
I have this method to get a generic repository out of a dictionary: public
I have two classes: Superclass and derived Subclass:Superclass. I have a generic method: public
I have a generic method define as below public T MyMethod<T>(extra params) My method
In my base class I have a generic method (ideally this would be a
I have this delegate declaration: public delegate IEnumerable<T> SearchInputTextStrategy<T, U>(string param); Lets assume I
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
In my class I have a static method public static void DoWork(int param) ...

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.