可以使用匿名内联代理的[CustomAttribute]吗? [英] Can one use [CustomAttribute] with anonymous in-line delegates?

查看:126
本文介绍了可以使用匿名内联代理的[CustomAttribute]吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用[attribute]符号,我可以将自定义属性附加到类实例方法。但是如果我编写代码如

With the [attribute] notation I can attach custom attributes to class instance methods. But if I write code like

menu.handlers[MOUSECLICK] += (clickEvent)delegate(event e) { ... };

[attribute]符号似乎不可用。我只限于声明为非法的方法的属性,还是有某种方式来使用委托(){...}声明的在线匿名方法?

the [attribute] notation seems not to be available. Am I limited to attributes for methods declared out of line, or is there a way to somehow do them for in-line anonymous methods declared using delegate() { ... }?

(我恰好喜欢内联样式:您可以轻松访问范围内的变量,我认为它可以使您更好,更紧凑的代码看看事件处理程序在同一个地方注册它会做什么...)

(I happen to like the inline style: you have easy access to in-scope variables and I think it makes for a nicer and more compact kind of code where you can see what the event handler will do at the same place where you register it...)

推荐答案


匿名方法是在线定义的,而不是
任何类的成员方法。另外,没有办法将自定义属性应用于匿名方法
,匿名方法也不能定义泛型类型
或添加通用约束。

The anonymous method is defined in-line and not as a member method of any class. Additionally, there is no way to apply custom attributes to an anonymous method, nor can the anonymous method define generic types or add generic constraints.

从MSDN引用文章 - 创建优雅的代码匿名方法,迭代器和部分类

Quoting from the MSDN Article - Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes

简而言之,不能为匿名方法添加自定义属性

In Short NO!, You cannot add custom attributes for Anonymous methods

这篇关于可以使用匿名内联代理的[CustomAttribute]吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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