问题描述
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?
推荐答案
动态关键字是新的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.MissingMethod()将编译并不会失败,直到运行时。此操作的结果是一个RuntimeBinderException因为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";
好了,你最有可能不会被写code像上面非常频繁。可能有的时间,但是,当变量重用可以派上用场,或清理脏一块遗留code。一个简单的情况下,我碰到经常不断有小数和双之间施展。
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");
了解更多功能:<一href="http://www.$c$cproject.com/KB/cs/CSharp4Features.aspx">http://www.$c$cproject.com/KB/cs/CSharp4Features.aspx
这篇关于什么是“动态”类型在C#4.0中使用的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!