所以,我们终于得到了VS2010一些开发商站的工作,可以使用C#4.0的功能。虽然大部分是我们发展仍然将不得不面向.NET 3.5暂且。
当我开始一个新项目,并设定目标到.NET 3.5,但它仍然允许我使用C#4.0,如动态。你因此可以使用C#4.0的功能,而目标定位.NET 3.5,并使用这些功能的环境中,.NET 4.0中不可用?
感谢。
解决方案动态如果你面向.NET 3.5框架code将无法编译。
要更加清晰,编译器将让你的定义和分配的一个动态变量,如
动态X = 3;这是一行code编译,因为动态只是编译成对象据类型考虑到了。但是,如果你再尝试的执行的任何与该变量,如:
Console.WriteLine(X);
...那么编译器将不得不产生code发现/要挟真正的类型,它不能做;你会得到下面的编译错误:
在C#编译器4依赖于DLR并专门Microsoft.CSharp组件有关的一切动态。这些都不是在.NET 3.5可用。因此,答案是否定的,你不能使用动态时,针对Framework 3.5版。
So we've finally got VS2010 on some developer stations at work and can use the C# 4.0 features. Although most of what we develop will still have to target .Net 3.5 for the time being.
When I start a new project and set the target to .Net 3.5, it still allows me to use C# 4.0 such as dynamic. Can you therefore use C#4.0 features whilst targetting 3.5 and will these features work in environments where .Net 4.0 is not available?
Thanks.
解决方案dynamic code will not compile if you target the .NET 3.5 framework.
To be more clear, the compiler will allow you to define and assign a dynamic variable, such as:
dynamic x = 3;That one line of code will compile, because dynamic just compiles to object as far as types are concerned. But if you then try to do anything with that variable, as in:
Console.WriteLine(x);... then the compiler would have to generate code to discover/coerce the real type, which it cannot do; you'll get the following compile errors:
The C# 4 compiler relies on the DLR and specifically the Microsoft.CSharp assembly for everything related to dynamic. These aren't available in .NET 3.5. So the answer is no, you cannot use dynamic when targeting Framework version 3.5.
更多推荐
C#4.0和.Net 3.5
发布评论