在一个匿名的lambda参数自定义属性 [英] Custom attribute on parameter of an anonymous lambda

查看:102
本文介绍了在一个匿名的lambda参数自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 ([MyCustomAttribute(...)]参数1,参数2)=> 
{
...

其中,参数1是Type1和参数2是类型2



在我看来,这是完全一样的。

 私人无效的方法等等([MyCustomAttribute(...)]类型参数1,类型2参数2)
{
...

但第一版本没有被C#编译器允许的。为什么?这是那些时间与精力/回报的事情的另一个呢?这是不是只是简单的不一致?


解决方案

为什么呢?




由于我乐于指出,语言设计团队不必为没有做一个功能的理由。相反,谁想要的功能让人不得不证明他们。



如果你问一个更有针对性的问题,而不是一个模糊的为什么?你可能会问:




如果我是在球场这个功能到C#设计团队,他们会用它确定是什么问题。




有是在lambda表达式被作为一个类的方法,在所有实施的C#语言没有要求。



首先,他们可能是表达式树,在这种情况下,又有什么意义有一个属性呢?第二,不存在的需求的这lambda实现为的编译器生成的closure类的,这意味着,有没有要求的参数列表,甚至是一些方法你可以把理智上的元数据。属性是程序的元数据的一部分,一个通常不认为浏览编译器生成的类型来消耗他们的元数据作为一个明智的做法的。



一个奇怪的,但完全可行的实施表达lambda表达式,比如,将始终生成表达式树,然后如果它原来不是一个表达式树的lambda ,生成对编译 A召唤出的另一端。没有元数据,所以没有地方放的属性。



这将是奇怪的,但C#语言经过精心设计,让喜欢lam​​bda表达式某些功能不限制编译器作家于特定的实现。




时的那些时间与精力此另一个/回收期东西呢?




是的。




是不是有人认为这虽然说,战胜饥饿国家联盟!?




据我所知。



正如我经常指出,在要实现为了一个功能它必须的 的思想。据我所知,你永远觉得对一个lambda参数的属性是一个好主意的人。想法没有得到的没有得到设计,实现,测试,文档或运思想。




这不只是简单的不一致?




我想是这样。


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

where param1 is Type1 and param2 is 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. 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?

There is no requirement in the C# language that lambdas be implemented as methods of a class at all.

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. 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. 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.

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. 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.

这篇关于在一个匿名的lambda参数自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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