c# 4.0和。net 3.5

时间:2021-02-11 21:46:51

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.

因此,我们终于在一些开发站点上安装了VS2010,并可以使用c# 4.0特性。尽管我们开发的大部分内容目前仍需要针对。net 3.5。

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 .net 3.5 and will these features work in environments where .Net 4.0 is not available?

当我启动一个新项目并将目标设置为。net 3.5时,它仍然允许我使用c# 4.0,比如dynamic。因此,您能在使用。net 3.5的同时使用c# 4.0特性吗?这些特性能在。net 4.0不可用的环境中工作吗?

Thanks.

谢谢。

1 个解决方案

#1


42  

dynamic code will not compile if you target the .NET 3.5 framework.

如果你的目标是。net 3.5框架,动态代码将不会编译。

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:

…然后编译器必须生成代码来发现/强制实际类型,这是它不能做的;您将得到以下编译错误:

  1. Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
  2. “Microsoft.CSharp.RuntimeBinder预定义的类型。绑定器'没有定义或导入
  3. 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?
  4. 无法找到编译动态表达式所需的一个或多个类型。你有没有漏掉微软csharp的参考资料?dll和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编译器依赖于DLR,特别是Microsoft。CSharp用于与动态相关的所有组件。这些在。net 3.5中没有。所以答案是否定的,你不能在针对框架3.5版本时使用动态。

#1


42  

dynamic code will not compile if you target the .NET 3.5 framework.

如果你的目标是。net 3.5框架,动态代码将不会编译。

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:

…然后编译器必须生成代码来发现/强制实际类型,这是它不能做的;您将得到以下编译错误:

  1. Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
  2. “Microsoft.CSharp.RuntimeBinder预定义的类型。绑定器'没有定义或导入
  3. 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?
  4. 无法找到编译动态表达式所需的一个或多个类型。你有没有漏掉微软csharp的参考资料?dll和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编译器依赖于DLR,特别是Microsoft。CSharp用于与动态相关的所有组件。这些在。net 3.5中没有。所以答案是否定的,你不能在针对框架3.5版本时使用动态。