“兼容包”用于向后移植新的 .NET Framework 功能?

发布于 2024-09-14 03:09:48 字数 1473 浏览 4 评论 0原文

由于各种原因,我经常发现编写与 .NET Framework 2.0 或 3.5 兼容或与 .NET Compact Framework 兼容的代码是可取的,但问题是新的 .NET 框架中有许多“小”功能在旧框架或 Compact Framework 中不可用。

例如,我发现扩展方法确实很有用,但编译器为此依赖于 System.Runtime.CompilerServices.ExtensionAttribute。您可以轻松地自己定义此属性,然后使用 .NET Framework 2.0(在 C# 3.0+ 下)中的扩展方法。同样,手动定义小型 .NET 4 类型(例如 TupleLazy)也不是太难。顺便说一句,如果你想在.NET 2.0中使用LINQ,你可以使用 LinqBridge

现在假设您将 ExtensionAttribute 设为公共,以便您编写的其他程序集可以使用它。一开始这很好,但是如果您随后想使用也有相同想法的第三方库怎么办?您添加了对该库的引用,现在您遇到了名称冲突。哎呀。

我还注意到,一些新库仅适用于 .NET 4.0 或 3.5,尽管它们对它只有较小的依赖性,可以使用兼容性包或 LinqBridge 来解决。

如果有针对较旧 .NET 版本的“兼容包”,在一个小 DLL 中定义这些小功能,您可以证明将其包含在任何大小的项目中,那肯定会很好。这样的事存在吗?

更新:从沉默来看,我猜没有这样的事情。如果有兴趣的话我可能会自己做一个这样的OSS库。所以我的新问题是,如果您为 .NET 2、.NET 3.5、.NETCF 或 Silverlight 编写代码,您会错过 .NET 4 的哪些较小功能(相对于 WCF/WPF 等庞然大物) ?我将从...

  • ExtensionAttribute(不在 .NET 2 中)
  • Func<...>Action<...> 开始列出。 委托(不在 .NET 2 中)
  • LINQ 到对象(不在 .NET 2 中)
  • Tuple<...> (不在 .NET 3.5 中)
  • Lazy< T>Lazy(.NET 3.5 中不存在)
  • 表达式树(.NET 2 中不存在;.NET 3.5 中不完整)
  • 泛型变量(.NET 2 中存在)但无法从 C# 3 和 VB 9 访问)
  • Reflection.Emit (.NETCF 中缺少;这并不是一个小功能,但我非常怀念它)

For various reasons I often find it desirable to write code that is compatible with .NET framework 2.0 or 3.5 or compatible with the .NET Compact Framework, but it is a problem that there are numerous "small" features in new .NET frameworks that are not available in the older frameworks or Compact Framework.

For example, I find extension methods are really useful, but the compiler depends on System.Runtime.CompilerServices.ExtensionAttribute for this. You can easily define this attribute yourself and then use extension methods in .NET Framework 2.0 (under C# 3.0+). Likewise, it isn't too hard to manually define little .NET 4 types like Tuple<T1,T2> and Lazy<T>. By the way, if you want to use LINQ in .NET 2.0, you can use LinqBridge.

Now suppose you make the ExtensionAttribute public so that other assemblies you write can use it. That's fine at first, but what if you then want to use a 3rd party library that also had the same idea? You add a reference to that library and now you've got a name collision. Oops.

I have also noticed that some new libraries are only available for .NET 4.0 or 3.5 even though they only have minor dependencies on it that could be resolved using a compatibility pack or LinqBridge.

It would sure be nice if there were "compatibility packs" for older .NET versions that defined these little features in a small DLL that you could justify including in projects of any size. Does such a thing exist?

Update: Judging by the silence, I guess there's no such thing. I might make such a OSS library myself if there is interest. So my new question is, what smaller features of .NET 4 (as opposed to monsters like WCF/WPF) would you miss if you were writing for .NET 2, .NET 3.5, .NETCF or Silverlight? I'll start the list off...

  • ExtensionAttribute (not in .NET 2)
  • Func<...> and Action<...> delegates (not in .NET 2)
  • LINQ-to-objects (not in .NET 2)
  • Tuple<...> (not in .NET 3.5)
  • Lazy<T> and Lazy<T,TMetadata> (not in .NET 3.5)
  • Expression Trees (not in .NET 2; incomplete in .NET 3.5)
  • Generics Variance (exists in .NET 2 but inaccessible from C# 3 and VB 9)
  • Reflection.Emit (missing from .NETCF; not really a small feature but I miss it very much)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜清冷一曲。 2024-09-21 03:09:48

Theraot 库

您可以使用 Theraot 库<中的 Theraot.Core /a> 通过条件编译将大部分 .NET 代码反向移植到从 .NET 2.0 开始的旧版本。

在上述功能中,还包括以下功能:

  • ExtensionAttribute
  • Func<...>Action<...> 委托
  • LINQ -to-objects
  • Tuple<...>
  • LazyLazy
  • 表达式 Tress

还包括以下内容问题中未提及的功能:

  • HashSet
  • SortedSet
  • ThreadLocal
  • IObservable > 和 IObserver
  • BigInteger
  • ConcurrentDictionary
  • 等...

注意:支持 System.Threading.Tasks已计划。

遗憾的是,在撰写本文时,可用的文档很少,但与 BCL 的行为的任何差异都可以被视为错误,并且可以 通过 github 报告

Theraot's Libraries

You can use use Theraot.Core from Theraot's Libraries to backport a great portion of .NET code to old versions starting with .NET 2.0 thanks to conditional compilation.

Out of the mentioned features, the following are included:

  • ExtensionAttribute
  • Func<...> and Action<...> delegates
  • LINQ-to-objects
  • Tuple<...>
  • Lazy<T> and Lazy<T,TMetadata>
  • Expression Tress

Also included are the following features not mentioned in the question:

  • HashSet<T>
  • SortedSet<T>
  • ThreadLocal<T>
  • IObservable<T> and IObserver<T>
  • BigInteger
  • ConcurrentDictionary<Tkey, TValue>
  • etc...

Note: Support for System.Threading.Tasks is planned.

Sadly, there is only little documentation available at the moment of writing, yet any difference on behavior from the BCL can be considered a bug, and can be reported via github.

み格子的夏天 2024-09-21 03:09:48

这并不是真正的“兼容性包”,但自从您提到 LinqBridge..我经常使用的另一个“向后移植功能”是在 适用于 Framework 3.5 SP1 的反应式扩展 (Rx)(位于 System.Threading.dll 中)。它包括任务并行库和并行 LINQ (PLINQ) 的完整实现。

对于 .Net 4.0,有 Visual Studio 2012 异步目标包 (nuget) 来自 Microsoft。它提供了许多异步扩展方法,并在使用 C# 5 编译器时提供对 async/await 关键字的支持。

类似地,对于 .Net 3.5,有 AsyncBridge 构建在反应式扩展的 TPL 库上,以提供异步/等待。还有一个用于 .Net 4.0 的 AsyncBridge 版本,但我不确定为什么您会想要该版本而不是 Microsoft 的版本。

This isn't really a "compatilibity pack", but since you mentioned LinqBridge... another "backported feature" I frequently use are the Parallel Extensions found (among other things) in the Reactive Extensions (Rx) for Framework 3.5 SP1 (in System.Threading.dll). It includes full implementation of the Task Parallel Library and Parallel LINQ (PLINQ).

For .Net 4.0 there is the Async Targeting Pack for Visual Studio 2012 (nuget) from Microsoft. Which provides many Async extention methods and provides support for the async/await keywords if using the C# 5 compiler.

Similarly for for .Net 3.5 there is the AsyncBridge that builds on the Reactive Extentions's TPL library to provide async/await. There is also a version of AsyncBridge for .Net 4.0, but I am not sure why you would want that one over the one from Microsoft.

草莓酥 2024-09-21 03:09:48

对于 .NET 3.5,您可以使用 .NET Framework 2.0 的 F# 运行时中的 FSharp.Core.dll。

“此可再发行包中包含的核心库 (FSharp.Core.dll) 包含系统命名空间中的一些 API,这些 API 与 F# 开发所需的 .NET Framework 4 API 相同。”

http://msdn.microsoft.com/en -us/library/ee829875%28v=vs.100%29.aspx

这包括 System.Tuple 等。和System.Lazy。 (但不是 Lazy。)要使用它们,只需引用 FSharp.Core.dll。

编辑:原来 FSharp.Core.dll 中的 Lazy 并不是直接替代品,而是更多的互操作类。它具有相同的属性,但构造函数不同。相反,它是这样创建的:

Lazy<string> lazy = Microsoft.FSharp.Control.LazyExtensions.CreateFromValue("test");

For .NET 3.5 you can use FSharp.Core.dll from the F# Runtime for .NET Framework 2.0.

"The core library (FSharp.Core.dll) included in this redistributable package contains some APIs in the System namespaces that are identical to .NET Framework 4 APIs that are required for F# development."

http://msdn.microsoft.com/en-us/library/ee829875%28v=vs.100%29.aspx

This includes System.Tuple et al. and System.Lazy<T>. (Not Lazy<T,TMetadata> though.) To use them just reference FSharp.Core.dll.

Edit: Turns out Lazy in FSharp.Core.dll is not a drop-in replacement, but more of an interop-class. It has the same properties but not the same constructors. Rather it's created e.g. like this:

Lazy<string> lazy = Microsoft.FSharp.Control.LazyExtensions.CreateFromValue("test");
翻身的咸鱼 2024-09-21 03:09:48

我不知道这样的列表会有多大用处,因为它的大小可能是巨大的。 2.0、3.0 和 2.0 的 CLR 是相同的。 3.5 因此从技术上讲,这些 2.0 后的功能中的任何一个都可以进入“兼容包”。

-奥辛

I don't know how useful such a list will be, as it is potentially monstrous in size. The CLR is the same for 2.0, 3.0 & 3.5 so tehnically any of these post-2.0 features could make their way into a "compatibilty pack."

-Oisin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文