title: How to write a roslyn analyzer that references a dotnet standard 2.0 project
body: <p>I have the following error from my Roslyn analyzer test case from the following project</p>
  
  <p><a href="https://github.com/Weingartner/Migrations.Json.Net" rel="noreferrer">https://github.com/Weingartner/Migrations.Json.Net</a></p>
  
  <blockquote>
    <p>Expected collection to be empty, but found {Test0.cs(5,2): error
    CS0012: The type 'Attribute' is defined in an assembly that is not
    referenced. You must add a reference to assembly 'netstandard,
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.}.</p>
  </blockquote>
  
  <p>The current metadata references in <a href="https://github.com/Weingartner/Migrations.Json.Net/blob/63686b00a83ca50cf6c1702e998b47520ac90022/Weingartner.Json.Migration.Roslyn.Spec/Helpers/DiagnosticVerifier.Helper.cs" rel="noreferrer">DiagnosticVerifier.Helper.cs</a> are</p>
  
  <pre><code>private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
  private static readonly MetadataReference SystemReference = MetadataReference.CreateFromFile(typeof(System.Uri).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 CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
  private static readonly MetadataReference SystemRuntimeReference = MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").Location);
  private static readonly MetadataReference MigrationReference = MetadataReference.CreateFromFile(typeof(MigratableAttribute).Assembly.Location);
  private static readonly MetadataReference SerializationReference = MetadataReference.CreateFromFile(typeof(DataMemberAttribute).Assembly.Location);
  private static readonly MetadataReference JsonNetReference = MetadataReference.CreateFromFile(typeof(Newtonsoft.Json.JsonConvert).Assembly.Location);
  </code></pre>
  
  <p>One of the references</p>
  
  <pre><code>private static readonly MetadataReference MigrationReference =
      MetadataReference
        .CreateFromFile(typeof(MigratableAttribute).Assembly.Location);
  </code></pre>
  
  <p>refers to a subproject <strong>Weingartner.Json.Migration</strong></p>
  
  <p><a href="https://i.stack.imgur.com/1W2Il.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1W2Il.png" alt="enter image description here"></a></p>
  
  <p>this project is a dotnetstandard 2.0 project</p>
  
  <pre><code>&lt;Project Sdk="Microsoft.NET.Sdk"&gt;
  
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netstandard2.0&lt;/TargetFramework&gt;
      &lt;RootNamespace&gt;Weingartner.Json.Migration&lt;/RootNamespace&gt;
      &lt;AssemblyName&gt;Weingartner.Json.Migration&lt;/AssemblyName&gt;
    &lt;/PropertyGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;PackageReference Include="Newtonsoft.Json" Version="10.0.3" /&gt;
    &lt;/ItemGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;ProjectReference Include="..\Weingartner.Json.Migration.Common_\Weingartner.Json.Migration.Common.csproj" /&gt;
    &lt;/ItemGroup&gt;
  
  &lt;/Project&gt;
  </code></pre>
  
  <p>However when I run the analyzer test case I get the runtime error that I should reference <strong>netstandard</strong></p>
  
  <blockquote>
    <p>Expected collection to be empty, but found {Test0.cs(5,2): error
    CS0012: The type 'Attribute' is defined in an assembly that is not
    referenced. You must add a reference to assembly 'netstandard,
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.}. 
    at FluentAssertions.Execution.XUnit2TestFramework.Throw(String
    message)    at
    FluentAssertions.Execution.AssertionScope.FailWith(String message,
    Object[] args)    at
    FluentAssertions.Execution.GivenSelector<code>1.FailWith(String message,
    Object[] args)    at
    FluentAssertions.Collections.CollectionAssertions</code>2.BeEmpty(String
    because, Object[] becauseArgs)    at
    TestHelper.DiagnosticVerifier.CreateProject(String[] sources, String
    language)    at TestHelper.DiagnosticVerifier.GetDocuments(String[]
    sources, String language)    at
    TestHelper.DiagnosticVerifier.GetSortedDiagnostics(String[] sources,
    String language, DiagnosticAnalyzer analyzer)    at
    TestHelper.DiagnosticVerifier.VerifyDiagnostics(String[] sources,
    String language, DiagnosticAnalyzer analyzer, DiagnosticResult[]
    expected)    at
    TestHelper.DiagnosticVerifier.VerifyCSharpDiagnostic(String source,
    DiagnosticResult[] expected)    at
    Weingartner.Json.Migration.Roslyn.Spec.DataContractAnalyzerSpec.ShouldCreateDiagnosticIfMigratableTypeDoesntHaveDataContractAttributeSet()</p>
  </blockquote>
  
  <p>My guess is I'm missing something at the point I create the project. I can't see an API to specify which version of the framework I'm targeting but the API is quite large so maybe I've missed something.</p>
  
  <pre><code>    private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp)
      {
          string fileNamePrefix = DefaultFilePathPrefix;
          string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;
  
          var projectId = ProjectId.CreateNewId(debugName: TestProjectName);
  
          var solution = new AdhocWorkspace()
              .CurrentSolution
              .AddProject(projectId, TestProjectName, TestProjectName, language)
              .AddMetadataReference(projectId, CorlibReference)
              .AddMetadataReference(projectId, SystemReference)
              .AddMetadataReference(projectId, SystemCoreReference)
              .AddMetadataReference(projectId, CSharpSymbolsReference)
              .AddMetadataReference(projectId, CodeAnalysisReference)
              .AddMetadataReference(projectId, SystemRuntimeReference)
              .AddMetadataReference(projectId, MigrationReference)
              .AddMetadataReference(projectId, SerializationReference)
              .AddMetadataReference(projectId, JsonNetReference);
          var compilationOptions = solution
              .GetProject(projectId)
              .CompilationOptions
              .WithOutputKind(OutputKind.DynamicallyLinkedLibrary);
          solution = solution
              .WithProjectCompilationOptions(projectId, compilationOptions);
  
          int count = 0;
          foreach (var source in sources)
          {
              var newFileName = fileNamePrefix + count + "." + fileExt;
              var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
              solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
              count++;
          }
          var project = solution.GetProject(projectId);
          var diangostics = project.GetCompilationAsync().Result.GetDiagnostics();
          diangostics.Should().BeEmpty();
          return project;
      }
  </code></pre>