What is the replacement for "MetadataReference.CreateAssemblyReference("mscorlib")"?

Topics: APIs
Apr 8, 2014 at 6:55 AM
I'm looking for the correct way to add a reference to an assembly in the GAC. Before the preview I would use the syntax "MetadataReference.CreateAssemblyReference("mscorlib")" to reference "mscorlib.dll". The syntax seems to have changed and the samples show using "typeof(object).Assembly.Location" but this is not nearly as nice as the old way. Is there an equivlent in the preview?
Developer
Apr 8, 2014 at 11:23 PM
Edited Apr 8, 2014 at 11:23 PM
MetadataReference.CreateAssemblyReference was removed because of its dependency on GAC APIs, which are not available on all platforms.
Use MetadataFileReference as demonstrated in the samples.
Apr 9, 2014 at 4:42 PM
In case anyone comes across this thread in the future I solved my problem like so:
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
                        
var compilation = CSharpCompilation.Create(assemblyName)
    .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
    .AddReferences(new MetadataFileReference(Path.Combine(assemblyPath, "mscorlib.dll")))
    .AddReferences(new MetadataFileReference(Path.Combine(assemblyPath, "System.dll")))
    .AddReferences(new MetadataFileReference(Path.Combine(assemblyPath, "System.Core.dll")))
    .AddReferences(new MetadataFileReference(Assembly.GetEntryAssembly().Location))
    .AddSyntaxTrees(syntaxTree);
Marked as answer by TomasMatousek on 4/9/2014 at 4:45 PM