C#4.0和.Net 3.5

编程入门 行业动态 更新时间:2024-10-27 13:31:28
本文介绍了C#4.0和.Net 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以,我们终于得到了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发现/要挟真正的类型,它不能做;你会得到下面的编译错误:

     
  • predefined型'Microsoft.CSharp.RuntimeBinder.Binder没有定义或导入
  •   
  • 要编译动态EX pression所需的一个或多个类型不能被发现。是否缺少引用Microsoft.CSharp.dll和System.Core.dll的?
  •   

    在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:

  • Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
  • One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?
  • 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

    本文发布于:2023-11-12 00:48:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1580032.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Net

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!