简体   繁体   English

使用 Visual Studio 2008 的 2.0 中的 C# .NET 3.0/3.5 功能

[英]C# .NET 3.0/3.5 features in 2.0 using Visual Studio 2008

What are some of the new features that can be used in .NET 2.0 that are specific to C# 3.0/3.5 after upgrading to Visual Studio 2008?升级到 Visual Studio 2008 后,可以在 .NET 2.0 中使用哪些特定于 C# 3.0/3.5 的新功能? Also, what are some of the features that aren't available?另外,还有哪些功能不可用?

Available可用的

  • Lambdas拉姆达
  • Extension methods (by declaring an empty System.Runtime.CompilerServices.ExtensionAttribute)扩展方法(通过声明一个空的 System.Runtime.CompilerServices.ExtensionAttribute)
  • Automatic properties自动属性
  • Object initializers对象初始值设定项
  • Collection Initializers集合初始化器
  • LINQ to Objects (by implementing IEnumerable extension methods, see LinqBridge ) LINQ to Objects(通过实现 IEnumerable 扩展方法,请参阅LinqBridge

Not Available无法使用

  • Expression trees表达式树
  • WPF/Silverlight Libraries WPF/Silverlight 库

You can use any new C# 3.0 feature that is handled by the compiler by emitting 2.0-compatible IL and doesn't reference any of the new 3.5 assemblies:您可以使用由编译器通过发出 2.0 兼容的 IL 处理的任何新的 C# 3.0 功能,并且不引用任何新的 3.5 程序集:

  • Lambdas (used as Func<..> , not Expression<Func<..>> ) Lambdas(用作Func<..> ,而不是Expression<Func<..>>
  • Extension methods (by declaring an empty System.Runtime.CompilerServices.ExtensionAttribute )扩展方法(通过声明一个空的System.Runtime.CompilerServices.ExtensionAttribute
  • Automatic properties自动属性
  • Object Initializers对象初始值设定项
  • Collection Initializers集合初始化器
  • LINQ to Objects (by implementing IEnumerable<T> extension methods, see LinqBridge ) LINQ to Objects(通过实现IEnumerable<T>扩展方法,请参阅LinqBridge

Pretty much everything!几乎所有的东西! Daniel Moth covers this here and here . Daniel Moth 在这里这里介绍了这一点 That only leaves runtime support: LINQ-to-Objects is provided by LINQBridge - which leaves just bigger APIs like Expression support, and tools like LINQ-to-SQL.只剩下运行时支持:LINQ-to-Objects 由LINQBridge提供 - 只剩下更大的 API,如表达式支持,以及 LINQ-to-SQL 等工具。 These are too big to be reasonably ported back to .NET 2.0, so I'd use .NET 3.5 for these.这些太大了,无法合理地移植回 .NET 2.0,所以我会使用 .NET 3.5 来实现这些。

I cover this in an article on my site .在我网站上一篇文章中介绍了这一点。

Almost all C# 3.0 features are available when targeting .NET 2.0.面向 .NET 2.0 时,几乎所有 C# 3.0 功能都可用。 For extension methods, you need to define an extra attribute.对于扩展方法,您需要定义一个额外的属性。 Expression trees aren't available at all.表达式树根本不可用。 Query expression support is based on a translation followed by "normal" C# rules, so you'll need something to provide the Select, Where etc methods.查询表达式支持基于后跟“正常”C# 规则的翻译,因此您需要提供 Select、Where 等方法。 LINQBridge is the de facto standard "LINQ to Objects in .NET 2.0" implementation. LINQBridge是事实上的标准“LINQ to Objects in .NET 2.0”实现。 You may well want to declare the delegates in the Func and Action delegate families to make it easier to work with lambda expressions - and then remove them if/when you move to .NET 3.5您可能希望在FuncAction委托系列中声明委托,以便更轻松地使用 lambda 表达式 - 然后如果/当您移动到 ​​.NET 3.5 时将其删除

To define extension methods, you'll need to supply the following class if you're targeting .NET 2.0:要定义扩展方法,如果您的目标是 .NET 2.0,则需要提供以下类:

namespace System.Runtime.CompilerServices {
  [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
    sealed class ExtensionAttribute : Attribute { }
}

There was a previous discussion about something similar you may also want to read too:之前有一个关于类似内容的讨论,您可能也想阅读:

Targeting .NET Framework 3.5, Using .NET 2.0 Runtime. 面向 .NET Framework 3.5,使用 .NET 2.0 运行时。 Caveats? 注意事项?

You can use Mono's version of the System.Core which fully supports LINQ & Expression Trees.您可以使用完全支持 LINQ 和表达式树的 Mono 版本的 System.Core。 I compiled its source against .net 2.0, and now I can use it in my .net2.0 projects.我针对 .net 2.0 编译了它的源代码,现在我可以在我的 .net2.0 项目中使用它。 This is great for projects that needs to be deployed on win2k, where .net3.5 is not available.这对于需要在 .net3.5 不可用的 win2k 上部署的项目非常有用。

Lambda 和扩展方法完全由编译器处理,可以与 .Net 2.0 框架一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM