简体   繁体   English

使用 System.Reflection 将参数传递给事件处理程序

[英]Passing parameter to event handler using System.Reflection

I have the following two programs (1) windows forms application (2) ClassLibrary1.dll The ClassLibrary1.dll is loaded to the winform through appdomain.我有以下两个程序 (1) windows forms 应用程序 (2) ClassLibrary1.dll 通过appdomain将ClassLibrary1.dll加载到winform中。 In windows forms, I have subscribed to event in ClassLibrary1.dll.在windows forms,我订阅了ClassLibrary1.dll中的事件。 I would like to pass a parameter to the event handler.我想将参数传递给事件处理程序。 But I am not sure how to do it.但我不知道该怎么做。 My trial below shows invalid expression.我下面的试验显示无效表达。 I couldn't find much references online.我在网上找不到太多参考资料。 Any help will be very much appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

This is the winform application code这是 winform 应用程序代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using ClassLibrary1;

namespace WindowsFormsApplication1
{
    
    [Serializable]
    public partial class Form1 : Form
    {
     
       
        public Form1()
        {
            InitializeComponent();
            Loader.Call(  "RaiseEvent",  DateTime.Now.ToShortDateString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Restart();
            Application.Run(new Form1());
            this.Close();
        }
    }

    public class Loader : MarshalByRefObject
    {
        static string dll = @"..\ConsoleApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
        static AppDomain ad = AppDomain.CreateDomain("Test");
        static Assembly a = Assembly.LoadFile(dll);
        static object o = a.CreateInstance("ClassLibrary1.Class1");
        static Type t = o.GetType();

        /***************************************************************************************************************/
        void HandleEvent(object sender,EventArgs e, int value)
        {
            Debug.WriteLine("HandleEvent called");
        }

        object CallInternal1(string method,  object[] parameters)
        {
            // Subscribe to the event
            EventInfo eventInfo1 = t.GetEvent("TestEvent");
            int val=2;
   //       eventInfo1.AddEventHandler(o,new EventHandler(object sender,EventArgs e) => HandleEvent(null,EventArgs.Empty,val));                         // invalid expression

            MethodInfo m = t.GetMethod(method);
            return m.Invoke(o, parameters);
        }
        /***************************************************************************************************************/
       

        public static object Call( string method,  params object[] parameters)
        {
            Loader ld = (Loader)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
            object result = 0;
            switch (method)
            {
                case "RaiseEvent":
                    {
                        result = ld.CallInternal1( method, parameters);
                        break;
                    }
            }
            return result;
        }
    }
} 

This is the ClassLibrary1.dll code:这是 ClassLibrary1.dll 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ClassLibrary1
{
    [Serializable]                                                
    public class Class1
    {
        public event EventHandler TestEvent;

        public int RaiseEvent(string msg)
        {
            try
            {
                TestEvent(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                Console.WriteLine("the exception is: " + ex.ToString());
                if (ex.InnerException != null)
                {
                    Console.WriteLine("the inner exception is: " + ex.InnerException.Message.ToString());
                }
            }
            return 2;
        }



       public event Action<int> TestAction = Func;               
        public int RaiseAct(string msg)
        {
            TestAction(3);                                       
            return 5;
        }

        public static void Func(int a)                          
        {
            int g = 2;
        }

    }
}

Do not realy sure, but in WinForms you must use current domain (AppDomain.CurrentDomain) instead of AppDomain.CreateDomain("Test").不太确定,但在 WinForms 中,您必须使用当前域 (AppDomain.CurrentDomain) 而不是 AppDomain.CreateDomain("Test")。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM