我可以使用.NET 4的功能,同时针对.NET 3.5 SP1?可以使用、功能、NET

2023-09-03 15:07:04 作者:温酒书生

我想用一些功能在.NET 4.0中,但仍Visual Studio 2010中在面向.NET 3.5基本上我想有这样的:

I would like to use some of the features in .NET 4.0 but still target .NET 3.5 within Visual Studio 2010. Basically I want to have something like:

if (.NET 4 installed) then
    execute .NET 4 feature

这是一个可选功能,我只是想它来运行,如果系统有.NET 4.0安装。如果系统只有.NET 3.5,那么该功能将不执行的是不是就是对应用非常关键。

This is an optional feature, and I would just like it to run if the system has .NET 4.0 installed. If the system only has .NET 3.5 then the feature would not execute as is it not something that is critical to the application.

推荐答案

首先,你必须针对3.5版本的框架,但通过具有应用使你的程序加载的4.0框架的.config ,看起来像这样(从How强制应用程序使用.NET 3.5或更高版本):

First of all, you have to target the 3.5 version of the framework but make your program loadable by the 4.0 framework by having an App.config that looks like this (from How to force an application to use .NET 3.5 or above?):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

至于如何激活4.0功能,这取决于你想要使用的功能。如果它是一个内置类的方法,你可以找它,使用它,如果它的存在。下面是在C#中的一个例子(它同样适用于VB):

As for how you activate the 4.0 feature, it depends on the feature you want to use. If it's a method on a built-in class, you can just look for it and use it if it exists. Here's an example in C# (it applies equally to VB):

var textOptions = Type.GetType("System.Windows.Media.TextOptions, " +
        "PresentationFramework, Version=4.0.0.0, " +
        "Culture=neutral, PublicKeyToken=31bf3856ad364e35");
if (textOptions != null)
{
    var setMode = textOptions.GetMethod("SetTextFormattingMode");
    if (setMode != null)
         // don't bother to lookup TextFormattingMode.Display -- we know it's 1
        setMode.Invoke(null, new object[] { this, 1 });
}

如果你把它放进你的主窗口的构造函数,它将设置 TextFormattingMode 显示在应用程序的.NET 4.0框架下运行,并且在3.5无能为力。

If you put this in your MainWindow constructor, it will set TextFormattingMode to Display in apps running under the .NET 4.0 framework and do nothing under 3.5.

如果你想使用一个类型,不可在3.5,你必须创建一个新的组件了。例如,创建一个类库项目目标定位4.0被称为因子与code这样的(你必须添加的引用System.Numerics;相同的C#声明):

If you want to use a type that isn't available in 3.5, you have to create a new assembly for it. For example, create a class library project targetting 4.0 called "Factorial" with code like this (you'll have to add a reference to System.Numerics; same C# disclaimer):

using System.Numerics;

namespace Factorial
{
    public class BigFactorial
    {
        public static object Factorial(int arg)
        {
            BigInteger accum = 1;  // BigInteger is in 4.0 only
            while (arg > 0)
                accum *= arg--;
            return accum;
        }
    }
}

然后创建一个项目,目标定位3.5,code这样的(相同的C#声明):

Then create a project targetting 3.5 with code like this (same C# disclaimer):

using System;
using System.Reflection;

namespace runtime
{
    class Program
    {
        static MethodInfo factorial;

        static Program()
        {   // look for Factorial.dll
            try
            {
                factorial = Assembly.LoadFrom("Factorial.dll")
                           .GetType("Factorial.BigFactorial")
                           .GetMethod("Factorial");
            }
            catch
            { // ignore errors; we just won't get this feature
            }
        }

        static object Factorial(int arg)
        {
            // if the feature is needed and available, use it
            if (arg > 20 && factorial != null)
                return factorial.Invoke(null, new object[] { arg });
            // default to regular behavior
            long accum = 1;
            while (arg > 0)
                accum = checked(accum * arg--);
            return accum;
        }

        static void Main(string[] args)
        {
            try
            {
                for (int i = 0; i < 25; i++)
                    Console.WriteLine(i + ": " + Factorial(i));
            }
            catch (OverflowException)
            {
                if (Environment.Version.Major == 4)
                    Console.WriteLine("Factorial function couldn't be found");
                else
                    Console.WriteLine("You're running " + Environment.Version);
            }
        }
    }
}

如果您复制EXE和Factorial.DLL到同一个目录并运行它,你就会得到所有在4.0前25阶乘,只有阶乘高达20随着错误消息的3.5(或者如果可以找不到该DLL)。

If you copy the EXE and Factorial.DLL into the same directory and run it, you'll get all the first 25 factorials under 4.0 and only the factorials up to 20 along with an error message on 3.5 (or if it can't find the DLL).