C#win:使用Reflection创建通用事件处理程序 [英] C# win: Using Reflection to create universal event handlers

查看:71
本文介绍了C#win:使用Reflection创建通用事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对使用反射很陌生,所以我有点难以置信。

我想通过Reflection附加一个事件处理程序(我能做到),但我想不要要知道确切的事件委托签名。


例如,我有一个.DLL,其中包含以下内容:

展开 | 选择 | Wrap | 行号

解决方案

好吧我还在作弊,但我正在取得进步。我现在可以创建我想要的事件,使用

中包含的类型给定(这保存在plater.dll中):

展开 | 选择 | Wrap | 行号


随时告诉我,如果我想念你的问题:


自定义事件args通常会为事件保存相关数据,并且他们应该从System.EventArgs继承 - 如果编码员遵循标准,我肯定你会这样做:oP


所以这将允许你在不知道正确签名的情况下附加处理程序的能力。但是 - 它不允许您访问 e 的自定义属性。


a la:

eventhandler MyGenericEventHandler(object发件人,EventArgs e)


object.event + = MyGenericEventHandler


为了访问自定义属性,您将需要把e转换成它的原生类型......在我们反思它之前我们都不知道。


我遇到这种情况之前我需要能够将对象转换为仅在运行时已知的类型,然后我遇到了墙。我会有另一个游戏,看看我能想出什么,但从以前的经验来看,我很怀疑我会成功...


或者你是试图抓住已在类中定义并将其附加到父应用程序的事件处理程序?也许我只是没有完全遵循你所追求的......


这是一个相当有趣的线程'试图达到同样的目的 - 尽管是不同的目的。这主要是有趣的,因为试图协助的人似乎完全忽略了试图获得帮助的人的观点,并且在得出没有合适的结论之前他们花了很多时间在圈子里跑来跑去......


但同意,这是你需要这种能力的情况。


虽然,我想你可以在反射对象上使用GetProperty和SetProperty - 这应该工作......这是他们所暗示的。不确定它是否会起作用,因为我还没有尝试过......


I''m a bit boggled by this since I am rather new to using reflection.
I want to attach an event handler via Reflection (which I can do), but I want to NOT have to know the exact event delegate signature.

For an exmaple, I have a .DLL with the following:

Expand|Select|Wrap|Line Numbers

解决方案

Well I am still cheating a lot, but I am making progress. I can now create the event I want, using types contained inside
Given (this is kept in plater.dll):

Expand|Select|Wrap|Line Numbers


Feel free to tell me if I''m missing what you''re asking:

Custom event args usually hold relevant data to the event, and they should inherit from System.EventArgs - if the coder follows standards, which I''m certain you do :oP

So that will allow you the ability of attaching a handler without knowing the correct signature. However - it won''t allow you access to the custom properties of e.

a la:
eventhandler MyGenericEventHandler(object Sender, EventArgs e)

object.event += MyGenericEventHandler

In order to access the custom properties, you''re going to need to cast e to its native type...which we can''t know until we reflect on it.

I''ve run into this situation before where I needed to be able to cast an object as a type only known at runtime and I came up against a wall. I''ll have another play and see what I can come up with but from previous experience I''m doubtful I''m going to be successful...

Or are you trying to grab the event handler that''s already defined inside the class and attach it to the parent application? Maybe I''m just not following quite what you''re after...


Here''s a rather amusing thread that''s trying to achieve the same thing - albeit for a different purpose. It''s mostly amusing because the person trying to assist seems to completely miss the point of the person trying to get assistance and they spend many posts running around in circles before coming up with no suitable conclusion...

But agreed, this is a situation where you would need this ability.

Although, I guess you could use GetProperty and SetProperty on the reflected object - that should work... which is what they allude to. Not sure if it''ll work though yet as I haven''t tried it...


这篇关于C#win:使用Reflection创建通用事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆