您将如何使用C#4动态类型? [英] How will you use the C# 4 dynamic type?

查看:118
本文介绍了您将如何使用C#4动态类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#4将包含一个新的动态关键字,将动态语言功能带入C#。



你计划在你自己的代码中使用它,你会提出什么模式?您当前项目的哪一部分将使您的代码更清洁或更简单,或者启用您不可以做的事情(在与IronRuby或IronPython之类的动态语言之间的明显互动之外)?


$ b $ PS:如果你不喜欢这个C#4,请避免发表评论。



编辑:重新整理问题。



大多数stackoverflow C#用户都熟悉了动态的经典用法。我想知道的是,如果你想到具体的新的C#模式,其中动态可以有效地利用,而不会失去太多的C#精神。

解决方案

现在使用老式反思,代码可读性受损。而且,正如你所说,一些Interop的场景(我偶尔使用COM)。



几乎是这样。如果动态使用可以避免,应该避免。编译时间检查,表现等。



几周前,我记得本文。当我第一次阅读它时,坦白地告诉我。但是我没有意识到的是,我不知道如何使用一个未知类型的运算符。我开始想知道生成的代码将是这样的:

  dynamic c = 10; 
int b = c * c;

使用定期反射,您不能使用定义的运算符。它生成了相当多的代码,使用一些 Microsoft 命名空间中的东西。让我们说上面的代码很容易阅读:)它很好,它的作品,但它也是非常慢:比常规乘法(doh)慢约10,000倍,约100倍比使用Multiply方法的 ICalculator 接口更慢。



编辑 - 生成的代码,对于那些感兴趣的人:

  if(< Test> o__SiteContainer0。<> p__Sitea == null)
< Test> o__SiteContainer0。<> p__Sitea =
CallSite&FunC< CallSite,object,object,object> .Create(
new CSharpBinaryOperationBinder(ExpressionType.Multiply,
false,false,new CSharpArgumentInfo [] {
new CSharpArgumentInfo(CSharpArgumentInfoFlags.None,null),
new CSharpArgumentInfo(CSharpArgumentInfoFlags.None,null)}));
b =< Test> o__SiteContainer0。<> p__Site9.Target(
< Test> o__SiteContainer0。<> p__Site9,
< Test> o__SiteContainer0。<> p__Sitea .Target(
< Test> o__SiteContainer0。<> p__Sitea,c,c));


C# 4 will contain a new dynamic keyword that will bring dynamic language features into C#.

How do you plan to use it in your own code, what pattern would you propose ? In which part of your current project will it make your code cleaner or simpler, or enable things you could simply not do (outside of the obvious interop with dynamic languages like IronRuby or IronPython)?

PS : Please if you don't like this C# 4 addition, avoid to bloat comments negatively.

Edit : refocussing the question.

The classic usages of dynamic are well known by most of stackoverflow C# users. What I want to know is if you think of specific new C# patterns where dynamic can be usefully leveraged without losing too much of C# spirit.

解决方案

Wherever old-fashioned reflection is used now and code readability has been impaired. And, as you say, some Interop scenarios (I occasionally work with COM).

That's pretty much it. If dynamic usage can be avoided, it should be avoided. Compile time checking, performance, etc.

A few weeks ago, I remembered this article. When I first read it, I was frankly apalled. But what I hadn't realised is that I didn't know how to even use an operator on some unknown type. I started wondering what the generated code would be for something like this:

dynamic c = 10;
int b = c * c;

Using regular reflection, you can't use defined operators. It generated quite a bit of code, using some stuff from a Microsoft namespace. Let's just say the above code is a lot easier to read :) It's nice that it works, but it was also very slow: about 10,000 times slower than a regular multiplication (doh), and about 100 times slower than an ICalculator interface with a Multiply method.

Edit - generated code, for those interested:

if (<Test>o__SiteContainer0.<>p__Sitea == null)
  <Test>o__SiteContainer0.<>p__Sitea =
    CallSite<Func<CallSite, object, object, object>>.Create(
      new CSharpBinaryOperationBinder(ExpressionType.Multiply,
        false, false, new CSharpArgumentInfo[] {
          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null),
          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null) }));
b = <Test>o__SiteContainer0.<>p__Site9.Target(
      <Test>o__SiteContainer0.<>p__Site9,
      <Test>o__SiteContainer0.<>p__Sitea.Target(
        <Test>o__SiteContainer0.<>p__Sitea, c, c));

这篇关于您将如何使用C#4动态类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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