.net - 我可以在面向 .NET 3.5 SP1 的同时使用 .NET 4 功能吗?

标签 .net vb.net visual-studio-2010 .net-3.5 .net-4.0

我想使用 .NET 4.0 中的一些功能,但仍以 Visual Studio 2010 中的 .NET 3.5 为目标。基本上我想要的是:

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

这是一个可选功能,如果系统安装了 .NET 4.0,我希望它运行。如果系统只有 .NET 3.5,那么该功能将不会执行,因为它对应用程序不是很重要。

最佳答案

首先,您必须以 3.5 版本的框架为目标,但通过使用 App.config 使您的程序可被 4.0 框架加载。看起来像这样(来自 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):
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 });
}

如果你把它放在你的 MainWindow构造函数,它将设置 TextFormattingModeDisplay在 .NET 4.0 框架下运行的应用程序中,在 3.5 下什么都不做。

如果要使用 3.5 中不可用的类型,则必须为其创建新程序集。例如,创建一个面向 4.0 的类库项目,名为“Factorial”,代码如下(您必须添加对 System.Numerics 的引用;相同的 C# 免责声明):
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 的项目(相同的 C# 免责声明):
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)。

关于.net - 我可以在面向 .NET 3.5 SP1 的同时使用 .NET 4 功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6119614/

相关文章:

c# - 达到最大连接池

c# - .NET WebBrowser - FireBug 样式检查 HTML 元素

.net - 使用 .NET Framework 安全系统

.net - 我自己的托管类型作为 C++/CLI 类库中的 arg : CS0570: is not supported by the language

c# - 当观察变量改变时暂停执行?

vb.net - 将常量数组传递给 VB.NET 中的函数

asp.net - 按值查找 TreeView 节点

vb.net - 这个 linq 查询有什么问题?

c# - 了解 Visual Studio 2012 负载测试中的页面响应时间(范围)值

visual-studio-2010 - visual studio 2010 CTP 到期解决方法