Q:

What is the correct way to load a portable MetadataReference in Roslyn?

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 MetadataReference objects.
For example, I can load the assemblies like this and it will work:
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
}

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:
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),

I get the following exceptions:

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'.

I believe this is because the solution running this code is a 4.6 assembly, so the typeof(object) is pointing to the 4.6 System.dll.
So my question is, what is the correct way to specify portable assemblies to use  as MetadataReference instances for a Roslyn compilation object?

A:

If you're compilation is referencing both the "full" framework 4.5 or 4.6, and you're also referencing portable libraries, you also need to add references to façade assemblies to bridge between them. You can find them at:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades

Where you should replace v4.6 with whatever version of the desktop framework you're targeting. You should just loop through that directory and add a reference for all the assemblies you see in there.

