C# 4.0 中的“动态"类型有什么用? [英] What is the 'dynamic' type in C# 4.0 used for?

查看:23
本文介绍了C# 4.0 中的“动态"类型有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# 4.0 引入了一种称为动态"的新类型.听起来不错,但程序员会用它做什么?

C# 4.0 introduced a new type called 'dynamic'. It all sounds good, but what would a programmer use it for?

有什么情况可以挽救这一天吗?

Is there a situation where it can save the day?

推荐答案

dynamic 关键字是 C# 4.0 的新增内容,用于告诉编译器变量的类型可以更改或直到运行时才知道.可以将其视为无需投射即可与对象进行交互.

The dynamic keyword is new to C# 4.0, and is used to tell the compiler that a variable's type can change or that it is not known until runtime. Think of it as being able to interact with an Object without having to cast it.

dynamic cust = GetCustomer();
cust.FirstName = "foo"; // works as expected
cust.Process(); // works as expected
cust.MissingMethod(); // No method found!

请注意,我们不需要将 cust 强制转换或声明为 Customer 类型.因为我们将其声明为动态的,运行时会接管并为我们搜索和设置 FirstName 属性.现在,当然,当您使用动态变量时,您就放弃了编译器类型检查.这意味着调用 cust.MissingMethod() 将编译并且直到运行时才会失败.此操作的结果是 RuntimeBinderException,因为在 Customer 类中未定义 MissingMethod.

Notice we did not need to cast nor declare cust as type Customer. Because we declared it dynamic, the runtime takes over and then searches and sets the FirstName property for us. Now, of course, when you are using a dynamic variable, you are giving up compiler type checking. This means the call cust.MissingMethod() will compile and not fail until runtime. The result of this operation is a RuntimeBinderException because MissingMethod is not defined on the Customer class.

上面的例子展示了动态在调用方法和属性时是如何工作的.另一个强大(且具有潜在危险)的功能是能够为不同类型的数据重用变量.我相信 Python、Ruby 和 Perl 程序员可以想出一百万种方法来利用这一点,但我一直在使用 C#,以至于我觉得它错了".

The example above shows how dynamic works when calling methods and properties. Another powerful (and potentially dangerous) feature is being able to reuse variables for different types of data. I'm sure the Python, Ruby, and Perl programmers out there can think of a million ways to take advantage of this, but I've been using C# so long that it just feels "wrong" to me.

dynamic foo = 123;
foo = "bar";

好的,所以您很可能不会经常编写上述代码.然而,有时变量重用可能会派上用场,或者清理一段肮脏的遗留代码.我经常遇到的一个简单情况是经常需要在十进制和双精度之间进行转换.

OK, so you most likely will not be writing code like the above very often. There may be times, however, when variable reuse can come in handy or clean up a dirty piece of legacy code. One simple case I run into often is constantly having to cast between decimal and double.

decimal foo = GetDecimalValue();
foo = foo / 2.5; // Does not compile
foo = Math.Sqrt(foo); // Does not compile
string bar = foo.ToString("c");

第二行无法编译,因为 2.5 被输入为双精度型,而第 3 行无法编译,因为 Math.Sqrt 需要双精度型.显然,您所要做的就是强制转换和/或更改您的变量类型,但在某些情况下,动态可能更适合使用.

The second line does not compile because 2.5 is typed as a double and line 3 does not compile because Math.Sqrt expects a double. Obviously, all you have to do is cast and/or change your variable type, but there may be situations where dynamic makes sense to use.

dynamic foo = GetDecimalValue(); // still returns a decimal
foo = foo / 2.5; // The runtime takes care of this for us
foo = Math.Sqrt(foo); // Again, the DLR works its magic
string bar = foo.ToString("c");

阅读更多功能:http://www.codeproject.com/KB/cs/CSharp4Features.aspx

这篇关于C# 4.0 中的“动态"类型有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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