简体   繁体   English

可以将[CustomAttribute]与匿名内联代理一起使用吗?

[英]Can one use [CustomAttribute] with anonymous in-line delegates?

With the [attribute] notation I can attach custom attributes to class instance methods. 使用[attribute]表示法,我可以将自定义属性附加到类实例方法。 But if I write code like 但是如果我写代码就好

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

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() { ... }? 我是否仅限于在线外声明的方法的属性,或者有没有办法以某种方式为使用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. 此外,无法将自定义属性应用于匿名方法,匿名方法也无法定义泛型类型或添加通用约束。

Quoting from the MSDN Article - Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes 引用MSDN文章 - 使用匿名方法,迭代器和部分类创建优雅代码

In Short NO!, You cannot add custom attributes for Anonymous methods 简而言之!,您无法为匿名方法添加自定义属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM