将 System.Web.Ui.WebControls 添加到分析器测试项目
Adding System.Web.Ui.WebControls to analyzer test project
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)
但我仍然得到类型 'Label' not is not defined from context.Compilation.GetDiagnostics(),这使我无法确定 label.Text 的类型。
我通过在 'c:\program files (x86)\reference assemblies\microsoft\framework.netframework\v4.0\' 目录中创建 2 个从 .net 框架获得的新引用并从路径加载来解决这个问题。
所以,我注释掉了
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
并加入
.AddMetadataReference(projectId, SystemCore)
.AddMetadataReference(projectId, SystemReference)
我还更改了从同一目录收集的 SystemWeb 参考。之后就成功了。
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)
但我仍然得到类型 'Label' not is not defined from context.Compilation.GetDiagnostics(),这使我无法确定 label.Text 的类型。
我通过在 'c:\program files (x86)\reference assemblies\microsoft\framework.netframework\v4.0\' 目录中创建 2 个从 .net 框架获得的新引用并从路径加载来解决这个问题。
所以,我注释掉了
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
并加入
.AddMetadataReference(projectId, SystemCore)
.AddMetadataReference(projectId, SystemReference)
我还更改了从同一目录收集的 SystemWeb 参考。之后就成功了。