简体   繁体   English

我应该使用类库来定位.NET Framework和C#版本?

[英]What .NET Framework and C# version should I target with my class library?

I'm building a DLL class library - I want to make it usable by as many people as possible. 我正在构建一个DLL类库 - 我希望尽可能多的人使用它。 Which version of the .NET Framework and which C# version should I use? 我应该使用哪个版本的.NET Framework和哪个C#版本? Is it possible to produce a backwards-compatible DLL or different DLLs for different versions? 是否可以为不同版本生成向后兼容的DLL或不同的DLL? Or does Windows automatically update the .NET framework so I should just use the latest version? 或者Windows是否自动更新.NET框架,所以我应该使用最新版本? Any guidance appreciated! 任何指导赞赏!

Personally, I'd target .NET 2.0. 就个人而言,我的目标是.NET 2.0。 This means, among other things: 这意味着,除其他外:

  • No extension methods (there is a workaround though) 没有扩展方法(虽然有解决方法)
  • No linq 没有linq

  • you CAN use lambda expressions 你可以使用lambda表达式

  • you CAN use the 'var' keyword 你可以使用'var'关键字

The thing is, you can use C# 3.x language features (the so-called syntactic sugar), but you can't use libraries that target C# 3.x (System.Core to name one, includes extension methods and linq). 问题是,您可以使用C#3.x语言功能(所谓的语法糖),但您不能使用以C#3.x为目标的库(System.Core为1,包括扩展方法和linq)。

I wouldn't try to support C# 1.x, as it's quite different from C# 2.x and higher. 我不会尝试支持C#1.x,因为它与C#2.x及更高版本完全不同。 Besides, I expect most people who would use your library are people building new things, who wouldn't in their right minds use C# 1.x ;-) 此外,我希望大多数使用你的图书馆的人都是建造新东西的人,他们不会在正确的思想中使用C#1.x ;-)

We target multiple runtime versions concurrently (.NET 1.1, .NET 2.0, and .NET 3.5) for some products. 我们针对某些产品同时针对多个运行时版本(.NET 1.1,.NET 2.0和.NET 3.5)。

We handle this in several ways: 我们以几种方式处理这个问题:

  • Separate Solution and Project files and for each of .NET 1.1, 2.0, and 3.5 SP1, but referencing the same source files. 单独的解决方案和项目文件以及.NET 1.1,2.0和3.5 SP1中的每一个,但引用相同的源文件。

eg: 例如:

\ProductFoo_1_1.sln (.NET 1.1 solution, VS 2003)
 \ProductFoo_2_0.sln (.NET 2.0 solution, VS 2008)
 \ProductFoo_3_5.sln (.NET 3.5 solution, VS 2008)

 \FooLibrary\FooLibrary_1_1.csproj (.NET 1.1 Project, VS 2003) 
 \FooLibrary\FooLibrary_2_0.csproj (.NET 2.0 Project, VS 2008) 
 \FooLibrary\FooLibrary_3_5.csproj (.NET 3.5 Project, VS 2008) 

 \FooLibrary\FooClass.cs (shared amongst all Projects)
 \FooLibrary\FooHelpers_1_1.cs (only referenced by the .NET 1.1 project)

 \FooService\FooService_3.5.csproj (.NET 3.5 Project, VS 2008)
 \FooService\FooService.cs
  • Defining NET_X_X symbols in each of the solutions 在每个解决方案中定义NET_X_X符号

  • For .NET Framework specific code, we use preprocessor instructions such as this: 对于.NET Framework特定代码,我们使用预处理器指令,例如:

public void SomeMethod(int param)
{
#ifdef NET_1_1
 // Need to use Helper to Get Foo under .NET 1.1
  Foo foo = Helper.GetFooByParam(param);
#elseif NET_2_0 || NET_3_5
 // .NET 2.0 and above can use preferred  method. 
  var foo =  new Foo { Prop = param }; 
  foo.LoadByParam();  
#endif 
  foo.Bar();
}

#ifdef NET_3_5
// A method that is only available under .NET 3.5 
public int[] GetWithFilter(Func Filter)
{ 
  // some code here
}
#endif

For clarification, the above lines starting with # are preprocessor commands. 为了澄清,以#开头的上述行是预处理器命令。 When you compile a solution, the C# Compiler (csc) pre-processes the source files. 编译解决方案时,C#编译器(csc)预处理源文件。 If you have an #ifdef statement, then csc will evaluate to determine if that symbol is defined - and if so, include the lines within that segment when compiling the project. 如果您有#ifdef语句,则csc将评估以确定是否定义了该符号 - 如果是,则在编译项目时包括该段中的行。

It's a way to mark up code to compile in certain conditions - we also use it to include more intensive debugging information in specific verbose debug builds, like so: 这是一种在某些条件下标记代码以进行编译的方法 - 我们还使用它在特定的详细调试版本中包含更密集的调试信息,如下所示:

#if DEBUG_VERBOSE
  Logging.Log("Web service Called with parameters: param = " + param);
  Logging.Log("Web service Response: " + response); 
  Logging.Log("Current Cache Size (bytes): " + cache.TotalBytes); 
  // etc. 
#endif
  • We then have NAnt scripts which automate the production of a release for each .NET version. 然后,我们使用NAnt脚本自动生成每个.NET版本的发行版。 We happen to control all this through TeamCity, but we can trigger the NAnt scripts manually too. 我们碰巧通过TeamCity控制所有这些,但我们也可以手动触发NAnt脚本。

It does make things more complicated, so we only tend to do it where we need to maintain a legacy .NET 1.1 or 2.0 instance (eg where a customer can't/won't upgrade). 它确实使事情变得更加复杂,因此我们只倾向于在需要维护旧版.NET 1.1或2.0实例的地方(例如,客户无法/不会升级)。

I imagine that when .NET 4.0 rolls around, we'll do the same thing and just add a NET_4_0 symbol. 我想当.NET 4.0滚动时,我们会做同样的事情,只需添加一个NET_4_0符号。

除非你需要使用3.0或3.5功能,否则我会保持2.0。

try this: 试试这个:

switch targetting mode to framework 2.0 (removing System.Core reference). 将目标模式切换到框架2.0(删除System.Core引用)。

if it not compile, than try adding a reference to linqbridge.dll : 如果没有编译,请尝试添加对linqbridge.dll的引用:

If not, then you should target 3.5 ;) 如果没有,那么你应该针对3.5;)

If I were to start a new project, I would always use the newest runtime! 如果我要开始一个新项目,我会一直使用最新的运行时! If 3.5 is available, why would I need to start a project in 2.0, or 1.0 unless I knew that there is something seriously wrong with the new version? 如果3.5可用,为什么我需要在2.0或1.0中启动项目,除非我知道新版本存在严重错误? New versions mean fixing old bugs and adding new features so this is good. 新版本意味着修复旧错误并添加新功能,这样做很好。

When it comes to upgrading old project to a new version, then you need to consider your gains and losses. 在将旧项目升级到新版本时,您需要考虑您的收益和损失。 If its worthed, upgrade it, if not stick with the old version. 如果它旋转,升级它,如果不坚持旧版本。

Be careful thought because new tools might not support older versions. 请小心,因为新工具可能不支持旧版本。 Though this is not the case with 2010 as it will support all version up to 2.0. 虽然2010年并非如此,因为它将支持高达2.0的所有版本。

I vote for Erik van Brakel's answer. 我投票支持Erik van Brakel的回答。 Also I would like to suggest that if you want to support 3.5 features like LINQ and Extension methods, you may create an additional library, say 另外我想建议如果你想支持LINQ和扩展方法等3.5功能,你可以创建一个额外的库

MyLibrary.DLL MyLibrary.DLL

MyLibrary.LINQ.dll MyLibrary.LINQ.dll

thus using the same approach as MS did (when they left System.dll 2.0 version but added all new features into System.Core.dll) 因此使用与MS相同的方法(当他们离开System.dll 2.0版本但将所有新功能添加到System.Core.dll中时)

我将使用包含核心功能的库来定位2.0版,并添加一个额外的目标库3.5,以根据您的核心库添加一些扩展方法。

从我的角度来看,如果你想要广泛的用户,你应该使用早期版本,1.1会很好,因为它可以在任何机器上运行.Net的版本。

It depends on what the dll is for. 这取决于dll的用途。 If it just has a generic C# logic that you would like to make available to others, then .net 2.0 is probably your best bet. 如果它只是一个你想让别人可以使用的通用C#逻辑,那么.net 2.0可能是你最好的选择。 However if it has anything to do with the newer features in .net like WPF, EF, WCF, silverlight, ect then it will need to be in the version of .net that supports that particular feature. 但是,如果它与.net中的较新功能有关,如WPF,EF,WCF,silverlight等,那么它将需要在支持该特定功能的.net版本中。

Personally, I would say write it in .net 3.5 only because making the jump from .net2.0 to .net3.5 is pretty painless because there is not many breaking changes unlike the jump from .net1.x to .net2.0. 就个人而言,我会说在.net 3.5中编写它只是因为从.net2.0到.net3.5的跳转是非常轻松的,因为与.net1.x到.net2.0的跳转不同,没有太多突破性的变化。 :) :)

I don't think anyone uses .Net 1.1 anymore. 我认为不再有人使用.Net 1.1了。 So unless you really want to use 3.5 features 2.0 should be fine. 所以除非你真的想要使用3.5功能2.0应该没问题。 Also if you have control over who's going to actually use your library then it depends on them too. 此外,如果您可以控制谁将实际使用您的库,那么它也取决于它们。 If they have the latest framework then you could use that. 如果他们有最新的框架,那么你可以使用它。

Combined with using an approach like the one mentioned by Will Hughes, if you want the access / option to use newer features when available, during active development, use the latest framework. 结合使用Will Hughes提到的方法,如果您希望访问/选项在可用时使用更新的功能,则在活动开发期间,使用最新的框架。 When ready to start making release candidates, set to the lowest framework, and then when issues arrive, steadily bump up the framework version and / or use the #ifdef approach to work out. 准备好开始发布候选版本时,设置为最低框架,然后当问题到达时,稳定地提升框架版本和/或使用#ifdef方法来解决问题。

This solution works quite well. 这个解决方案效果很好。 I just set up two different projects each with a unique "Project Properties->Build->Conditional compilation Symbols" and used in the code like this: 我只是设置了两个不同的项目,每个项目都有一个独特的“Project Properties-> Build-> Conditional compilation Symbols”,并在代码中使用如下:

#if NET_4
            xmlReaderSettings.DtdProcessing = DtdProcessing.Ignore; 
#endif
#if NET_3_5
            xmlReaderSettings.ProhibitDtd = false;                
#endif

暂无
暂无

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

相关问题 我应该在C#程序中使用.NET Framework 3.5检测Windows 8.1的正确操作系统版本? - What should I do in my C# program to detect the correct OS version for Windows 8.1, using .NET Framework 3.5? .Net Core库应该针对哪个.Net框架? - Which .Net framework should my .Net Core library target? 我应该为 .NET Core 中的库项目选择哪个目标框架 - Which target framework should I choose for library project in .NET Core 我应该在我的类库中包含Entity Framework还是直接包含在应用程序中? - Should I include Entity Framework in my class library or directly into application? C#版本的.NET Framework - C# - version of .NET framework .NET - 我目前在哪个版本的框架中运行(来自C#) - .NET - what version of the framework am I currently running in (from C#) 什么是用于指定.net框架版本的C#编译器选项 - what is the C# compiler option for specifying the version of .net framework 如何使用类库(.NET Framework)在C#中创建Excel文件? - How create file excel in C# with Class Library (.NET Framework)? 如何为C#类库多目标以同时支持NET_STANDARD和NET_COREAPP? - How can I multi-target a C# Class Library to support both NET_STANDARD and NET_COREAPP? 修复 WARNING: The target version of the .NET Framework in the project does not match the .NET Framework launch condition version C# Setup Project - Fix the WARNING: The target version of the .NET Framework in the project does not match the .NET Framework launch condition version C# Setup Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM