0

TL:DR -- How do I add System.Web.Ui.WebControls to my AdhocWorkspace?

Long version...

My unit test for an analyzer is failing because the test compilation environment doesn't have a reference to System.Web.Ui.WebControls. I need to get a semantic type, and that is failing because it doesn't know what the type is.

My test project is 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>

My DiagnosticVerifier has:

    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);

And adds them to the workspace via:

        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)

But I'm still getting a Type 'Label' not is not defined from context.Compilation.GetDiagnostics(), which prevents me from determining what the type of the label.Text.

jmoreno
  • 12,752
  • 4
  • 60
  • 91

1 Answers1

0

I solved this by creating 2 new references obtained from the .net framework in the 'c:\program files (x86)\reference assemblies\microsoft\framework.netframework\v4.0\' directory and loading from the path.

So, I commented out

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

and added in

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

I also changed the SystemWeb reference to be gathered from the same directory. After that it worked.

jmoreno
  • 12,752
  • 4
  • 60
  • 91