我有一个winform应用程序(WindowsFormsApplication2),它使用appdomain加载dll。在winform中,我引用了ClassLibrary.dll中实现的'TestEvent'。如果TestEvent返回一个参数,我如何在winform中实现的事件处理程序HandleEvent中捕获返回的参数?
这是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;
using static System.Net.Mime.MediaTypeNames;
namespace WindowsFormsApplication2
{
[Serializable]
public partial class Form1 : Form
{
void HandleEvent(object sender, EventArgs e)
{
Debug.WriteLine("HandleEvent called");
}
string DLL = @"..\ConsoleApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
public Form1()
{
InitializeComponent();
Loader.Call( DLL, "ClassLibrary1.Class1", "RaiseEvent", HandleEvent, DateTime.Now.ToShortDateString());
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
Application.Run(new Form1());
this.Close();
}
}
public class Loader : MarshalByRefObject
{
AppDomain ad = AppDomain.CreateDomain("Test");
object CallInternal(string dll, string typename, string method, EventHandler handler, object[] parameters)
{
Assembly a = Assembly.LoadFile(dll);
object o = a.CreateInstance(typename);
Type t = o.GetType();
// Subscribe to the event
EventInfo eventInfo = t.GetEvent("TestEvent");
eventInfo.AddEventHandler(o, handler);
MethodInfo m = t.GetMethod(method);
return m.Invoke(o, parameters);
}
public static object Call( string dll, string typename, string method, EventHandler handler, params object[] parameters)
{
Loader ld = (Loader)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
object result = ld.CallInternal(dll, typename, method, handler, parameters);
AppDomain.Unload(ad);
return result;
}
}
}
这是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;
}
}
}
2条答案
按热度按时间aurhwmvo1#
您可以使用类似于以下内容的内容:
How do I use reflection to call a generic method?
Invoke a method of anonymous class
Subscribe to an event with Reflection
Invoke static methods
qzwqbdag2#
据我所知,您希望您的
HandleEvent
方法能够使用或生成一些我们称为Argument
的自定义值,在这种情况下,可以通过为自定义事件继承EventArgs
来实现结果。自定义事件参数类
然后,您可以使用以下方法之一处理事件:
请给我一个评论,如果这是从你试图实现的方式。