Pass extra parameters to an event handler?

后端 未结 8 855
执笔经年
执笔经年 2020-11-22 15:31

Let\'s say I want to pass some extra data when assigning an event handler. Consider the following code:

private void s         


        
相关标签:
8条回答
  • 2020-11-22 15:46

    you can try doing this:

    string yourObject;
    
    theClassWithTheEvent.myEvent += (sender, model) =>
    {
     yourObject = "somthing";
    }
    
    0 讨论(0)
  • 2020-11-22 15:52

    Well, the simplest method id to make someData a member variable like so:

    public class MyClass
    {
        private string _eventData;
    
        private void setup(string someData) 
        {
           _eventData = someData;
           Object.assignHandler(evHandler);
        }
    
        public void evHandler()
        {
            // do something with _eventData here
        }
    }
    

    I'm not sure that's the best way to do it, but it really depends on the event type, the object, etc.

    0 讨论(0)
  • 2020-11-22 15:53

    My question that was similar was marked a duplicate so thought I'd add an answer here since it won't let me on my question.

    class Program
        {
            delegate void ComponentEventHandler(params dynamic[] args);
    
            event ComponentEventHandler onTest;
    
            static void Main(string[] args)
            {  
                Program prg = new Program();
    
                // can be bound to event and called that way
                prg.onTest += prg.Test;
                prg.onTest.Invoke("What", 5, 12.0);
    
                Console.ReadKey();
            }
    
            public void Test(params dynamic[] values)
            {
                // assign our params to variables
                string name = values[0];
                int age = values[1];
                double value = values[2];
    
                Console.WriteLine(name);
                Console.WriteLine(age);
                Console.WriteLine(value);
            }
        }
    
    0 讨论(0)
  • 2020-11-22 15:54

    Here is my one-line solution that pass extra parameters to a timer handler.

    private void OnFailed(uint errorCode, string message)
    {
        ThreadPoolTimer.CreateTimer((timer) => {
        UI.ErrorMessage = string.Format("Error: 0x{0:X} {1}", errorCode, message);
        }, System.TimeSpan.FromMilliseconds(100));
    }
    
    0 讨论(0)
  • 2020-11-22 15:56

    You could create a custom object having additional properties based on Object:

    class CustomObject : Object
    {
        public string SomeData;
    }
    
    private void setup(string someData)
    {
        CustomObject customObject = new CustomObject { SomeData = someData };
        CustomObject.assignHandler(evHandler);
    }
    
    public void evHandler(Object sender)
    {
        string someData = ((CustomObject)sender).SomeData;
    }
    

    If the data should not be changed anymore after initialization, you could also add a custom constructor, for example.

    0 讨论(0)
  • 2020-11-22 16:02
    private void setup(string someData)
    {
         Object.assignHandler((sender) => evHandler(sender,someData));
    }
    public void evHandler(Object sender, string someData)
    {
        // need someData here!!!
    }
    
    0 讨论(0)
提交回复
热议问题