C# 属性修改方法

发布于 2024-08-09 19:15:42 字数 180 浏览 10 评论 0原文

全部。也许我没有足够的谷歌搜索,但我找不到关于这个问题的任何例子。

在 C# 中是否可以创建一个自定义属性,该属性应用于类并修改其所有方法?例如,添加 Console.WriteLine("Hello, i'm modded method"); 作为第一行(或者如果在运行时完成,则它是 IL 等效项)?

all. Maybe i've not googled enough, but i can't find any example on this question.

It is possible in C# to create an custom attribute which, applied to a class, modifies all of its methods? For example, adds Console.WriteLine("Hello, i'm modified method"); as a first line (or it's IL equivalent if it's done at runtime)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2024-08-16 19:15:42

是的,您可以执行此操作,但不行,它不是内置于 C# 中的。正如 Eric 所说,这种技术被称为面向方面编程。

我在工作中使用过PostSharp,它非常有效。它在编译时工作,并使用 IL 编织,而不是其他 AOP 技术。

例如,以下属性将执行您想要的操作:

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method | MulticastTargets.Class,
                         AllowMultiple = true,
                         TargetMemberAttributes = MulticastAttributes.Public | 
                                                  MulticastAttributes.NonAbstract | 
                                                  MulticastAttributes.Managed)]
class MyAspect : OnMethodInvocationAspect
{
    public override void OnInvocation(MethodInvocationEventArgs eventArgs)
    {
        Console.WriteLine("Hello, i'm modified method");

        base.OnInvocation(eventArgs);
    }
}

您只需将 MyAspect 作为类的属性应用,它将应用于其中的每个方法。您可以通过修改 MulticastAttributeUsage 属性的 TargetmemberAttributes 属性来控制应用方面的方式。在此示例中,我想将其限制为仅适用于公共非抽象方法。

您还可以做更多事情,请看一下(一般来说,在 AOP 中)。

Yes, you can do this, but no, its not built in to C#. As Eric says, this technique is known as Aspect Oriented Programming.

I've used PostSharp at work, and it is very effective. It works at compile time, and uses IL-weaving, as opposed to other AOP techniques.

For example, the following attribute will do what you want:

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method | MulticastTargets.Class,
                         AllowMultiple = true,
                         TargetMemberAttributes = MulticastAttributes.Public | 
                                                  MulticastAttributes.NonAbstract | 
                                                  MulticastAttributes.Managed)]
class MyAspect : OnMethodInvocationAspect
{
    public override void OnInvocation(MethodInvocationEventArgs eventArgs)
    {
        Console.WriteLine("Hello, i'm modified method");

        base.OnInvocation(eventArgs);
    }
}

You simply apply MyAspect as an attribute on your class, and it will be applied to every method within it. You can control how the aspect is applied by modifying the TargetmemberAttributes property of the MulticastAttributeUsage property. In this example, I want to restrict it to apply only to public, non-abstract methods.

There's a lot more you can do so have a look (at AOP in general).

蓝天 2024-08-16 19:15:42

不,您正在寻找的是面向方面的编程(AOP)。

使用 AOP,您可以指定切入点、要编织代码的位置以及要在该点执行的代码。跟踪是 AOP 的标准示例。您指定一组方法和编织器/编译器以在该方法的开头或结尾添加日志/跟踪调用。

No. What you are looking for is aspect oriented programming (AOP).

With AOP you specify a pointcut, a place where you want to weave in code, and the code you want to executed at that point. Tracing is the standard example for AOP. You specify a set of methods and the the weaver/compiler to add you log/tracing call at the beginning or the end of that methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文