C#属性上调用一个方法触发事件 [英] C# Attribute to trigger an event on invoking a method

查看:889
本文介绍了C#属性上调用一个方法触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在C#或.NET一般创建用于触发调用一个方法,当一个事件的方法的属性?理想情况下,我就可以行动前,该方法的调用之后,以自定义运行。

Is there a way in C# or .NET in general to create an attribute on a method which triggers an event when a method is invoked? Ideally, I would be able to run custom actions before and after the invocation of the method.

我的意思是这样的:

[TriggersMyCustomAction()]
public void DoSomeStuff()
{
}

我完全无能如何做到这一点,或者如果它在所有可能的,但<一个href="http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx">System.Diagnostic.ConditionalAttribute可能会做类似的事情的背景。我不知道,但。

I am totally clueless how to do it or if it possible at all, but System.Diagnostic.ConditionalAttribute might do a similar thing in the background. I am not sure though.

修改:我忘了提的是,由于我的具体情况而定,性能不是一个真正的问题。

EDIT: I forgot to mention that due to the circumstances of my specific case, performance is not really an issue.

推荐答案

我知道如何做到这一点的唯一方法是使用 PostSharp 的。它的后处理您的IL和可以做的事情一样,你问什么。

The only way I know how to do this is with PostSharp. It post-processes your IL and can do things like what you asked for.

这篇关于C#属性上调用一个方法触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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