如何添加属性在运行时的方法? [英] How do I add attributes to a method at runtime?
问题描述
我们正在使用Microsoft.Practices.CompositeUI.EventBroker来处理我们的应用程序事件订阅和发布。该工作方式是添加一个属性到你的活动,指定主题名称,如:
We're using Microsoft.Practices.CompositeUI.EventBroker to handle event subscription and publication in our application. The way that works is that you add an attribute to your event, specifying a topic name, like this:
[EventPublication("example", PublicationScope.Global)]
public event EventHandler Example;
那么另一个属性添加到您的处理程序,使用相同的主题名称,例如:
then you add another attribute to your handler, with the same topic name, like this:
[EventSubscription("example", ThreadOption.Publisher)]
public void OnExample(object sender, EventArgs e)
{
...
}
然后,你将你的对象相匹配的一切行动的EventInspector。
Then you pass your objects to an EventInspector which matches everything up.
我们需要调试这个,所以我们正在试图创建订阅的所有的事件调试类。我可以得到所有的题目的名单......但只有在运行时。所以,我需要能够将属性在运行时添加的方法,我们通过我们的调试对象的EventInspector之前。
We need to debug this, so we're trying to create a debug class that subscribes to all the events. I can get a list of all the topic names... but only at runtime. So I need to be able to add attributes to a method at runtime, before we pass our debug object to the EventInspector.
我如何在运行时添加属性的方法?
How do I add attributes to a method at runtime?
推荐答案
你所要实现的目标是相当复杂的,所以我会尽量提供的东西只是为了让你开始。这是我认为你需要,以实现的东西结合起来:
What you are trying to achieve is quite complicated, so I will try to provide something just to get you started. This is what I think you would need to combine in order to achieve something:
- 定义一个抽象类
AbstractEventDebugger
与搜索
的方法,搜索所有的事件
成员,并与EventInspector注册它们。另外,定义一个方法IdentifyEvent
,将允许您以确定它称为事件(这取决于你 - 什么参数都会有,等)。 - 定义
动态类型
使用TypeBuilder
(所描述的这里),从你的类继承。这个类是班级的调试
对象。 - 使用附加的处理程序,以类
Reflection.Emit.MethodBuilder
(见的here ),这将是调用父类的IdentifyEvent
方法,并在 -
Reflection.Emit的
使用CustomAttributeBuilder
类中的处理程序的属性(见<一href=\"http://msdn.microsoft.com/en-us/library/system.reflection.emit.customattributebuilder.aspx\">here). - 创建您的
动态
类的一个实例,并将其发送到EventInspector。 - 火起来
:)
- Define an abstract class
AbstractEventDebugger
, with a methodSearch
that searches all of theevent
members, and registers them with the EventInspector. Also, define a methodIdentifyEvent
that will allow you to identify the event that has called it (this depends on you - what parameters will have, etc.). - Define a
dynamic type
usingTypeBuilder
(as described here), that inherits from your class. This class would be the class of yourdebugger
object. - Attach the Handlers to your class using
Reflection.Emit.MethodBuilder
(see here), which will be calling theIdentifyEvent
method from the parent class and, Reflection.Emit
the attributes on the handlers usingCustomAttributeBuilder
class (see here).- Create an instance of your
dynamic
class and send it to the EventInspector. - Fire it up
:)
这里是如何创建一个调用的东西的方法的样本(其实这是典型的Hello World的)。
Here is a sample on how to create a method that calls something (Actually it's the classic "Hello world").
您将需要做大量的调整,以便把它做得很好,但你会学到很多反思。
You will need to do a lot of tweaking in order to get it done well, but you will learn a lot about reflection.
祝你好运!
这篇关于如何添加属性在运行时的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!