"兼容包"为向后移植新的.NET框架的功能?框架、功能、QUOT、NET

2023-09-07 02:25:01 作者:我们演的戏都不看剧本。

由于种种原因,我经常发现需要写code,它与.NET Framework 2.0或3.5或兼容.NET Compact Framework的兼容,但它是一个问题,有无数的小的特点不在旧的框架或提供新的.NET框架Compact Framework的。

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.

例如,我发现扩展方法是真正有用的,但是编译器依赖于 System.Runtime.CompilerServices.ExtensionAttribute 这一点。您可以轻松地定义这个属性的自己,然后使用扩展方法在.NET Framework 2.0中(在C#3.0+)。同样的,这是不是太硬手动定义小.NET 4类型,如元组LT; T1,T2> 延迟< T> 。顺便说一句,如果你想使用LINQ在.NET 2.0中,可以使用 LinqBridge 。

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.

现在假设您做出 ExtensionAttribute 公开,这样其他组件编写可以使用它。这很好在第一,但如果你再要使用第三方库,也有同样的想法?您添加的引用到库,现在你已经有了一个名称冲突。哎呀。

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.

我也注意到了一些新的库仅适用于.NET 4.0或3.5,即使他们只有在它小的依赖性,可以使用兼容包或LinqBridge予以解决。

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.

这将肯定是很好,如果有兼容包为定义在一个小的DLL,你可以证明,包括在任何规模的项目,这些小功能较旧的.NET版本。有没有这样的事情存在?

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?

更新:由沉默来看,我想有没有这样的事情。我可能会做出这样的开源软件库自己是否有兴趣。所以我的新问题是,什么样的小的的.NET 4(而不是像WCF / WPF的怪物)你会错过,如果你正在写的.NET 2,.NET 3.5,.NETCF或Silverlight功能?我将开始清单关闭...

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 (而不是在.NET 2) Func键&LT; ...&GT; 动作&LT; ...&GT; 代表(不在.NET 2) LINQ到对象(而不是在.NET 2) 元组LT; ...&GT; (而不是在.NET 3.5) 延迟&LT; T&GT; 延迟&LT; T,TMetadata&GT; (而不是在.NET 3.5) 防爆pression树木(不是在.NET 2,不完整的.NET 3.5) 在泛型差异(存在于.NET 2,但在C#3和VB 9人迹罕至) Reflection.Emit的(从.NETCF丢失;不是一个真正的小功能,但我十分想念它) 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)

推荐答案

Theraot的库

您可以使用使用 Theraot.Core 从 Theraot的图书馆来的backport的.NET code到旧版本有很大一部分开始.NET 2.0感谢条件编译。

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键&LT; ...&GT; 动作&LT; ...&GT; 代表 LINQ到对象 元组LT; ...&GT; 延迟&LT; T&GT; 延迟&LT; T,TMetadata&GT; 防爆pression发辫 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&LT; T&GT; 的SortedSet&LT; T&GT; 的ThreadLocal&LT; T&GT; 的IObservable&LT; T&GT; IObserver&LT; T&GT; 的BigInteger ConcurrentDictionary&LT; TKEY,TValue&GT; 等等... HashSet<T> SortedSet<T> ThreadLocal<T> IObservable<T> and IObserver<T> BigInteger ConcurrentDictionary<Tkey, TValue> etc...

注:支持 System.Threading.Tasks 计划

可悲的是,唯一可用的文档很少在写作的时刻,然而从BCL可以被认为是一个错误的行为有什么区别,并且可以的通过github上报道。

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.