首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过反射添加BackgroundWorker RunWorkerCompleted事件?

如何通过反射添加BackgroundWorker RunWorkerCompleted事件?
EN

Stack Overflow用户
提问于 2009-07-14 11:37:05
回答 3查看 2.9K关注 0票数 0

通常我会说:

代码语言:javascript
复制
bgwExportGrid.RunWorkerCompleted += ReportManager.RunWorkerCompleted;

ReportManager类是一个静态类,其中包含我要使用的事件处理程序。

代码语言:javascript
复制
public static class ReportManager
{
        public static void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        ...
        }
}

现在,我已经创建了一个BackgroundWorker,并希望按照ReportManager中的定义附加RunWorkerCompleted事件。但是,不能引用ReportManager,否则会发生循环引用,因此需要反射。

任何帮助都将不胜感激。

我已经看过以下内容,但还没有走得太远:

代码语言:javascript
复制
        Assembly assem = Utils.GetAssembly("WinUI.Reporting.Common.dll");
        Type reportManagerType = assem.GetModule("WinUI.Reporting.Common.dll").GetType("WinUI.Reporting.Common.ReportManager");
        EventInfo evWorkerCompleted = reportManagerType.GetEvent("RunWorkerCompleted");
        Type tDelegate = evWorkerCompleted.EventHandlerType;
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-07-14 12:21:27

我认为,如果您将接口从ReportManager缩减为两个程序集都可以引用的接口,那么将来您的代码将更容易维护。但是,如果这对你来说不是一个选择,我认为你正在尝试实现这样的事情:

代码语言:javascript
复制
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AttachEventHandler(backgroundWorker1,
                  Type.GetType("WindowsFormsApplication1.EventHandlers"),
                  "RunWorkerCompleted");
            backgroundWorker1.RunWorkerAsync();
        }

        private void AttachEventHandler(BackgroundWorker bgw, Type targetType,
            string eventHandlerMethodName)
        {
            object targetInstance = Activator.CreateInstance(targetType);
            bgw.RunWorkerCompleted += 
                (RunWorkerCompletedEventHandler)Delegate.CreateDelegate(
                    typeof(RunWorkerCompletedEventHandler), 
                    targetInstance, eventHandlerMethodName);
        }

    }

    public class EventHandlers
    {
        public void RunWorkerCompleted(object sender, 
            System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            // do something 
        }
    }
}

请注意,Form1EventHandlers类之间没有“硬”引用,因此可以是驻留在任何其他程序集中的任何其他类;事件处理程序是根据类型的名称和方法的名称创建和附加的(当然,它们必须具有正确的签名)。

票数 4
EN

Stack Overflow用户

发布于 2009-07-14 11:41:36

更新答案:

代码语言:javascript
复制
Assembly assem = Utils.GetAssembly("WinUI.Reporting.Common.dll");

Type reportManagerType = assem.GetModule("WinUI.Reporting.Common.dll").GetType("WinUI.Reporting.Common.ReportManager");

// obtain the method info
MethodInfo mi = reportManagerType.GetMethod("RunWorkerCompleted", 
                                            BindingFlags.Static | BindingFlags.Public);

// create a delegate that we can further register with the event
Delegate handler = Delegate.CreateDelegate(reportManagerType , mi);

// get the event info from the export grid object
EventInfo evWorkerCompleted = bgwExportGrid.GetType().GetEvent("RunWorkerCompleted");

// invoke the event
evWorkerCompleted.AddEventHandler(bgwExportGrid, handler);
票数 0
EN

Stack Overflow用户

发布于 2009-07-14 13:13:27

设法让它工作起来了。ReportManager是一个静态类,因此不需要使用Activator.CreateInstance。

代码语言:javascript
复制
        Assembly assem = Utils.GetAssembly("WinUI.Reporting.Common.dll");
        Type reportManagerType = assem.GetModule("WinUI.Reporting.Common.dll").GetType("WinUI.Reporting.Common.ReportManager");
        bgwExportGrid.RunWorkerCompleted += (RunWorkerCompletedEventHandler)Delegate.CreateDelegate(typeof(RunWorkerCompletedEventHandler), reportManagerType, "RunWorkerCompleted");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1124843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档