You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7 IIncrementalGenerator测试报错:找不到Xunit.Sdk.EqualException构造方法

问题描述

测试.NET 7 C#的IIncrementalGenerator源代码生成器时,持续抛出异常:

System.MissingMethodException : Method not found: 'Void Xunit.Sdk.EqualException..ctor(System.Object, System.Object)'

已尝试将当前程序集引用加载到编译中,但仍未定位到问题根源。

测试类代码

public class SourceGeneratorVerifier : SourceGeneratorTest<XUnitVerifier> {
    public override string Language => LanguageNames.CSharp;

    protected override CompilationOptions CreateCompilationOptions() => new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

    protected override GeneratorDriver CreateGeneratorDriver(Project project, ImmutableArray<ISourceGenerator> sourceGenerators) => CSharpGeneratorDriver.Create(sourceGenerators, project.AnalyzerOptions.AdditionalFiles, (CSharpParseOptions)project.ParseOptions!, project.AnalyzerOptions.AnalyzerConfigOptionsProvider);

    protected override ParseOptions CreateParseOptions() => new CSharpParseOptions(LanguageVersion.Default, DocumentationMode.Diagnose);

    protected override IEnumerable<ISourceGenerator> GetSourceGenerators() { yield return new LampCodeGenerator().AsSourceGenerator(); }

    protected override string DefaultFileExt => "cs";

    public static Task RunTestAsync(string source, string expected, params string[] path) {
        var assemblies = new ReferenceAssemblies("net7.0", new PackageIdentity("Microsoft.NETCore.App.Ref", "7.0.0"), Path.Combine("ref", "net7.0"));

        var test = new SourceGeneratorVerifier {
            TestState = {
                ReferenceAssemblies = assemblies,
                Sources = {
                    source
                },
                GeneratedSources = {
                    ("GeneratorAttributes.g.cs", "...."),
                    (Path.Combine(path), expected)
                }
            }
        };

        test.TestState.AdditionalReferences.Add(typeof(SourceGeneratorVerifier).Assembly);

        return test.RunAsync();
    }
}

测试项目引用配置

<ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
    <PackageReference Include="FluentAssertions" Version="6.12.0" />
    <PackageReference Include="xunit" Version="2.5.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="6.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeRefactoring.Testing.XUnit" Version="1.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" PrivateAssets="all" />
  </ItemGroup>
解决方案

这个异常是xunit版本与代码分析测试包版本不兼容导致的,具体解决步骤如下:

  • 版本匹配检查:Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit 1.1.1版本依赖的xunit版本低于你使用的2.5.1,xunit 2.5.x移除了EqualException(Object, Object)构造函数,导致测试运行时找不到该方法。
  • 调整版本适配:
    • 方案一:降低xunit版本到2.4.2(与测试包1.1.1兼容的稳定版本),修改项目引用中的xunit和xunit.runner.visualstudio版本为2.4.2。
    • 方案二:升级代码分析测试包到兼容xunit 2.5.x的版本,比如将Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit及相关的Analyzer/CodeFix/CodeRefactoring测试包升级到1.3.0或更高版本(需确保所有测试包版本一致)。
  • 清理冗余引用:移除不必要的重复测试包引用,确保所有代码分析测试包版本完全统一,避免版本冲突。
  • 清理重建:修改版本后,删除项目的bin/obj目录,重新生成解决方案,避免旧版本dll残留引发的问题。

内容的提问来源于stack exchange,提问作者Gargoyle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 02:06:00