title: What is the correct way to load a portable MetadataReference in Roslyn?
body: <p>I am trying to compile some code that is built against the portable .Net libraries and I am trying to determine the correct way to load the portable assemblies as <code>MetadataReference</code> objects.</p>
  
  <p>For example, I can load the assemblies like this and it will work:</p>
  
  <pre><code>var analyzerCode = "// Some analyzer code here";
  var syntaxTree = CSharpSyntaxTree.ParseText(analyzerCode);
  
  string assemblyName = System.IO.Path.GetRandomFileName();
  MetadataReference[] references = new MetadataReference[]
  {
      MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Runtime.dll"),
      MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Diagnostics.Debug.dll"),
      MetadataReference.CreateFromFile(typeof(System.Collections.Immutable.ImmutableArray).Assembly.Location),
      MetadataReference.CreateFromFile(typeof(Microsoft.CodeAnalysis.CSharp.CSharpCompilation).Assembly.Location),
      MetadataReference.CreateFromFile(typeof(Microsoft.CodeAnalysis.Workspace).Assembly.Location)
  };
  
  CSharpCompilation compilation = CSharpCompilation.Create(assemblyName, new[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
  
  using (var ms = new MemoryStream())
  {
      EmitResult result = compilation.Emit(ms);
      // Check the result for errors and work with it
  }
  </code></pre>
  
  <p>Obviously this is not very portable code as I am using file paths that may only work on my machine.  If I do the following:</p>
  
  <pre><code>MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
  </code></pre>
  
  <p>I get the following exceptions:</p>
  
  <blockquote>
    <p>CS0012: The type 'Object' is defined in an assembly that is not
    referenced. You must add a reference to assembly 'System.Runtime,
    Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.</p>
  </blockquote>
  
  <p>I believe this is because the solution running this code is a 4.6 assembly, so the <code>typeof(object)</code> is pointing to the 4.6 System.dll.</p>
  
  <p>So my question is, what is the correct way to specify portable assemblies to use  as <code>MetadataReference</code> instances for a Roslyn compilation object?</p>