反射参数名:C#的拉姆达前pressions或语法的辉煌滥用? [英] Reflecting parameter name: abuse of C# lambda expressions or Syntax brilliance?

查看:171
本文介绍了反射参数名:C#的拉姆达前pressions或语法的辉煌滥用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看的 MvcContrib Grid组件,我很着迷,但在同一时间击退,由在<一个使用的语法伎俩href=\"http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/\">Grid语法:

I am looking at the MvcContrib Grid component and I'm fascinated, yet at the same time repulsed, by a syntactic trick used in the Grid syntax:

.Attributes(style => "width:100%")

上面的语法设置生成的HTML以宽度样式属性:100%。现在,如果你注意,'风格'是无处规定,从在名称在EX pression参数的推断!我不得不钻研,发现这个地方魔力发生了:

The syntax above sets the style attribute of the generated HTML to width:100%. Now if you pay attention, 'style' is nowhere specified, is deduced from the name of the parameter in the expression! I had to dig into this and found where the 'magic' happens:

   Hash(params Func<object, TValue>[] hash)
   {
     foreach (var func in hash)
     {
       Add(func.Method.GetParameters()[0].Name, func(null));
     }
   }

所以事实上,code使用的是正式的,编译时,参数的名称创建的属性名称值对的字典。所得的语法结构是非常前pressive确实,但在同一时间非常危险的。一般使用的lambda前pressions允许更换无副作用使用的名称的的。我看到一本​​书,说一个例子 collection.ForEach(书=&GT; Fire.Burn(书))我知道我可以在我的code写的 collection.ForEach(日志=&GT; Fire.Burn(日志))这意味着同样的事情的。但这里所有的突然的MvcContrib电网语法我发现code的积极寻找,并根据我选择我的变量名decissions!

So indeed, the code is using the formal, compile time, name of parameters to create the dictionary of attribute name-value pairs. The resulted syntax construct is very expressive indeed, but at the same time very dangerous. The general use of lambda expressions allows for replacement of the names used without side effect. I see an example in a book that says collection.ForEach(book => Fire.Burn(book)) I know I can write in my code collection.ForEach(log => Fire.Burn(log)) and it means the same thing. But with the MvcContrib Grid syntax here all of the sudden I find code that actively looks and makes decissions based on the names I choose for my variables!

所以是用C#3.5 / 4.0社会和拉姆达前pressions爱好者这个共同的做法呢?或者是一个无赖伎俩之一特立独行我不应该担心?

So is this common practice with the C# 3.5/4.0 community and the lambda expressions lovers? Or is a rogue one trick maverick I shouldn't worry about?

推荐答案

这具有互操作性较差。例如,考虑这个C# - F#的例子

This has poor interop. For example, consider this C# - F# example

C#:

public class Class1
{
    public static void Foo(Func<object, string> f)
    {
        Console.WriteLine(f.Method.GetParameters()[0].Name);
    }
}

F#

Class1.Foo(fun yadda -> "hello")

结果:

ARG印(不是yadda)。

"arg" is printed (not "yadda").

因此​​,图书馆的设计师要么避免这些类型的'虐待'的,要不然至少提供了一个'标准'过载(例如,是以字符串名称作为一个额外的参数),如果他们想跨越具有良好的互操作。网络语言。

As a result, library designers should either avoid these kinds of 'abuses', or else at least provide a 'standard' overload (e.g. that takes the string name as an extra parameter) if they want to have good interop across .Net languages.

这篇关于反射参数名:C#的拉姆达前pressions或语法的辉煌滥用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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