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

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

问题描述

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

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

怎么办你打算在自己的代码中使用它,你会提出什么样的模式?在当前项目的一部分,将它使你的代码的清洁剂或简单,或使事情你可以简单地这样做(外像的IronRuby或IronPython的动态语言的明显互操作的)?

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:请,如果你不喜欢这个C#4此外,避免产生负面膨胀的评论

编辑:再聚焦的问题。

动态经典用法深受大多数计算器C#用户闻名。如果你想在哪里动态可以在不失去太多的C#精神被有效利用特定的新C#的模式我想知道的是。

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.

推荐答案

无论现在老式反射的使用和代码的可读性已经发生减值。而且,正如你说的,有些互操作场景(我偶尔使用COM工作)。

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.

几个星期前,我想起的this文章。当我第一次读它,我坦率地apalled。但我没有意识到的是,我不知道怎么连的使用的操作上的一些未知类型。我开始想生成的代码会是怎样的东西是这样的:

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;

使用正反射,则不能使用定义操作。它产生相当多的代码,用一些东西从微软命名空间。远的不说上面的代码是一个更容易阅读:)这是很好,它的工作原理,但也有人的非常的慢:比普通乘法(DOH)慢约10000倍,约100倍,比用乘法方法的 ICalculator 接口速度较慢

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天全站免登陆