如何添加属性在运行时的方法? [英] How do I add attributes to a method at runtime?

查看:135
本文介绍了如何添加属性在运行时的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Microsoft.P​​ractices.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:


  1. 定义一个抽象类 AbstractEventDebugger 搜索的方法,搜索所有的事件成员,并与EventInspector注册它们。另外,定义一个方法 IdentifyEvent ,将允许您以确定它称为事件(这取决于你 - 什么参数都会有,等)。

  2. 定义动态类型使用 TypeBuilder (所描述的这里),从你的类继承。这个类是班级的调试对象。

  3. 使用附加的处理程序,以类 Reflection.Emit.MethodBuilder (见的here ),这将是调用父类的 IdentifyEvent 方法,并在

  4. Reflection.Emit的使用 CustomAttributeBuilder 类中的处理程序的属性(见<一href=\"http://msdn.microsoft.com/en-us/library/system.reflection.emit.customattributebuilder.aspx\">here).

  5. 创建您的动态类的一个实例,并将其发送到EventInspector。

  6. 火起来:)

  1. Define an abstract class AbstractEventDebugger, with a method Search that searches all of the event members, and registers them with the EventInspector. Also, define a method IdentifyEvent that will allow you to identify the event that has called it (this depends on you - what parameters will have, etc.).
  2. Define a dynamic type using TypeBuilder (as described here), that inherits from your class. This class would be the class of your debugger object.
  3. Attach the Handlers to your class using Reflection.Emit.MethodBuilder (see here), which will be calling the IdentifyEvent method from the parent class and,
  4. Reflection.Emit the attributes on the handlers using CustomAttributeBuilder class (see here).
  5. Create an instance of your dynamic class and send it to the EventInspector.
  6. 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屋!

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