将System.Web.Ui.WebControls添加到分析器测试项目

时间:2019-05-24 13:26:40

标签: .net-core roslyn-code-analysis

TL:DR-如何将System.Web.Ui.WebControls添加到我的AdhocWorkspace?

长版...

我的分析器单元测试失败,因为测试编译环境没有对System.Web.Ui.WebControls的引用。我需要获取一个语义类型,但是失败了,因为它不知道该类型是什么。

我的测试项目是core2.0:

 <PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
 </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis" Version="3.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.2" />
    <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="3.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="3.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
    <PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
    <PackageReference Include="System.Data.Common" Version="4.3.0" />
  </ItemGroup>

我的DiagnosticVerifier具有:

    private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
    private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location);
    private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
    private static readonly MetadataReference VBLangSymbolsReference = MetadataReference.CreateFromFile(typeof(VisualBasicCompilation).Assembly.Location);
    private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
    private static readonly MetadataReference SystemData = MetadataReference.CreateFromFile(typeof(System.Data.DataRow).Assembly.Location);
    private static readonly MetadataReference SystemWeb = MetadataReference.CreateFromFile(typeof(System.Web.HttpUtility).Assembly.Location);

并通过以下方式将它们添加到工作区中:

        var solution = new AdhocWorkspace()
            .CurrentSolution
            .AddProject(projectId, TestProjectName, TestProjectName, language)
            .AddMetadataReference(projectId, CorlibReference)
            .AddMetadataReference(projectId, SystemCoreReference)
            .AddMetadataReference(projectId, CSharpSymbolsReference)
            .AddMetadataReference(projectId, VBLangSymbolsReference)
            .AddMetadataReference(projectId, CodeAnalysisReference)
            .AddMetadataReference(projectId, SystemData)
            .AddMetadataReference(projectId, SystemWeb)

但是我仍然从context.Compilation.GetDiagnostics()中未定义类型'Label',这使我无法确定label.Text的类型。

1 个答案:

答案 0 :(得分:0)

我通过在'c:\ program files(x86)\ referenceassembly \ microsoft \ framework.netframework \ v4.0 \'目录中创建从.net框架获得的2个新引用并从路径加载来解决了这一问题。

所以,我注释掉了

.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)

并添加到

.AddMetadataReference(projectId, SystemCore)
.AddMetadataReference(projectId, SystemReference)

我还更改了从同一目录收集的SystemWeb参考。之后,它起作用了。