简体   繁体   English

匿名lambda参数的自定义属性

[英]Custom attribute on parameter of an anonymous lambda

([MyCustomAttribute(...)] param1, param2) =>
{
    ...

where param1 is Type1 and param2 is Type2 其中param1是Type1,param2是Type2

It seems to me this is exactly the same as 在我看来,这完全一样

private void method blah([MyCustomAttribute(...)] Type1 param1, Type2 param2)
{
    ...

But the first version is not allowed by the C# compiler. 但是C#编译器不允许使用第一个版本。 Why? 为什么? Is this another one of those time vs effort/payback things ? 这是另一个时间与努力/回报的事情吗? Isn't this just plain inconsistent? 这不是简单的不一致吗?

Why? 为什么?

As I am fond of pointing out, the language design team does not have to provide a justification for not doing a feature. 正如我喜欢指出的那样,语言设计团队不必为不执行功能提供理由。 Rather, people who want features have to justify them. 相反,想要功能的人必须证明他们的合理性。

If you were to ask a more targeted question instead of a vague "why?" 如果你要问一个更具针对性的问题,而不是模糊的“为什么?” you might ask: 你可能会问:

If I were to pitch this feature to the C# design team, what problems would they identify with it? 如果我将此功能推荐给C#设计团队,他们会发现什么问题呢?

There is no requirement in the C# language that lambdas be implemented as methods of a class at all. 在C#语言中没有要求将lambdas实现为类的方法。

First of all, they could be expression trees, in which case, what sense does it make to have an attribute there? 首先,它们可能是表达式树,在这种情况下,在那里有一个属性有什么意义? And second, there is no requirement that a lambda be implemented as a method of a compiler-generated closure class , which means that there's no requirement that the parameter list even be something that you can sensibly put metadata on. 第二,没有要求将lambda实现为编译器生成的闭包类的方法 ,这意味着不要求参数列表甚至是可以合理地放置元数据的东西。 Attributes are part of the metadata of the program, and one does not normally think of browsing compiler-generated types to consume their metadata as a sensible thing to do. 属性是程序元数据的一部分,人们通常不会想到浏览编译器生成的类型来将其元数据作为一种明智的做法。

A strange but perfectly workable implementation of expression lambdas, for example, would be to generate the expression tree always, and then if it turned out to not be an expression tree lambda, generate a call to Compile out the other end. 例如,表达式lambdas的一个奇怪但完全可行的实现是始终生成表达式树,然后如果事实证明它不是表达式树lambda,则生成对另一端Compile的调用。 No metadata, so no place to put the attribute. 没有元数据,所以没有放置属性的地方。

That would be strange, but the C# language has been carefully designed so that certain features like lambdas do not constrain the compiler writer to a particular implementation. 这很奇怪,但C#语言经过精心设计,因此像lambdas这样的某些功能不会将编译器编写器限制在特定的实现中。

Is this another one of those time vs effort/payback things? 这是另一个时间与努力/回报的事情吗?

Yes. 是。

Did someone think this though and say, naah !? 有人这么想,并说,呐!

Not to my knowledge. 据我所知。

As I frequently point out, in order for a feature to be implemented it has to be thought of . 正如我经常指出的那样,为了实现一个功能,必须考虑到它 To my knowledge you're the first person to ever think that an attribute on a lambda parameter is a good idea. 据我所知,你是第一个认为lambda参数属性是个好主意的人。 Ideas that don't get thought of don't get designed, implemented, tested, documented or shipped. 没有想到的想法不会被设计,实施,测试,记录或发送。

Isn't this just plain inconsistent? 这不是简单的不一致吗?

I suppose so. 我想是这样。

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

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