使用.NET 3.5的Visual Studio 2010项目似乎允许System.Xml中仅可从.NET 4.0获得的方法

tor*_*ldr 3 .net c# visual-studio-2010 .net-3.5

我最近在某些机器上遇到了以下异常.

未处理的异常:System.MissingMethodException:找不到方法:'Void System.Xml.Xsl.XslCompiledTransform.Transform(System.Xml.XPath.IXPathNavigable,System.Xml.Xsl.XsltArgumentList,System.Xml.XmlWriter,System.Xml.XmlResolver )".

从这个错误可以清楚地看出,我们正在尝试在System.Xml.Xsl.XslCompiledTransform不存在的类上使用方法.

该错误似乎与Windows XP计算机隔离,无论是否存在.NET Framework 4.0,但在我尝试过的Windows 7计算机上都可以运行(所有计算机都有3.5和4.0).

阅读文档,我们可以看到这里使用的签名在.NET 4.0中可用,但不是更早.

在新的.NET 3.5控制台应用程序中使用以下代码,我尝试用几种不同的方式编译项目.

using System;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace SystemXmlTestTool
{
    class Program
    {
    static void Main(string[] args)
    {
        var transform = new XslCompiledTransform();

        try
        {
            transform.Transform((IXPathNavigable)null, null, null, null);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.GetType().Name);
            Console.WriteLine(e.Message);
        }

        Console.ReadKey();
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过运行

C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe /target:clean
C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe /target:build
Run Code Online (Sandbox Code Playgroud)

在Windows 7机器上(以及通过Visual Studio 2010进行编译),项目将成功编译.

如果我在Windows XP机器上安装了同样的程序(也安装了.NET Framework 4.0),我会收到编译器警告,告诉我很明显.

这一切归结为的问题是; 为什么Windows 7机器上的Visual Studio 2010/MSBuild(多个)允许我在我的代码中使用此方法,而它不应该出现在.NET Framwork 3.5中?

我怀疑我的环境有问题,但我不知道为什么.

Dan*_*rth 7

最有可能的是,您只在其失效的计算机上安装了.NET Framework 3.5 Client Profile,并在其运行的计算机上安装了完整的.NET Framework 3.5.

我可以在定位完整框架时编译Windows 7上的代码,但不能在定位客户端配置文件时编译代码.

此时文档似乎不正确.

  • @MarcGravell:看起来客户端配置文件具有某些程序集的特殊版本.在dotPeek中打开c:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\System.XML.dll表明,实际上,此方法不存在. (2认同)