我应该用我的类库的目标是什么.NET Framework和C#版本? [英] What .NET Framework and C# version should I target with my class library?

查看:188
本文介绍了我应该用我的类库的目标是什么.NET Framework和C#版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个DLL的类库 - 我想,以使其可被尽可能多的人越好。我应该使用哪个版本的.NET Framework和C#版本的?是有可能产生用于不同版本的向后兼容的DLL或不同的DLL?还是Windows会自动所以我应该只使用最新版本更新.NET框架?任何指导AP preciated!

I'm building a DLL class library - I want to make it usable by as many people as possible. Which version of the .NET Framework and which C# version should I use? Is it possible to produce a backwards-compatible DLL or different DLLs for different versions? Or does Windows automatically update the .NET framework so I should just use the latest version? Any guidance appreciated!

推荐答案

我们的目标同时运行多个版本(.NET 1.1,.NET 2.0和.NET 3.5)。

We target multiple runtime versions concurrently (.NET 1.1, .NET 2.0, and .NET 3.5) for some products.

我们在几个方面处理这个问题:

We handle this in several ways:


  • 独立的解决方案和项目文件,并为每个.NET 1.1,2.0和3.5 SP1,但引用相同的源文件。

例如:


 \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


  • 定义 NET_X_X 符号在每个解决方案

    有关.NET框架的具体code,我们使用preprocessor指令,例如这样的:

    For .NET Framework specific code, we use preprocessor instructions such as this:

    
    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 
    

    有关澄清,以#开头的上述行是preprocessor命令。当你编译一个解决方案,C#编译器(CSC)pre-过程的源文件。
    如果你有一个 #IFDEF 语句,则CSC将评估,以确定是否该符号的定义 - 如果是这样,编译项目时,包含段内的线路。

    For clarification, the above lines starting with # are preprocessor commands. When you compile a solution, the C# Compiler (csc) pre-processes the source files. 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.

    这是来标记code在一定的条件下编译的方式 - 我们还用它来包括具体详细的调试更加密集的调试信息构建,像这样:

    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 
    


    • 我们再有这些自动化生产的每一个.NET版本释放的恶性脚本。
      我们通过发生到TeamCity的控制这一切,但我们可以触发恶性脚本手动了。

    • 这确实使事情变得更加复杂,所以我们只倾向于这样做,我们需要保持一个传统的.NET 1.1或2.0实例(如凡客不能/不会升级)。

      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 4.0来临时,我们会做同样的事情,只需添加NET_4_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 Framework和C#版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆