Joginder S Nahil
Joginder S Nahil

Reputation: 837

How to use MetadataReference.CreateFromAssembly instead of MetadataFileReference

I downloaded package C# Code Rendering with Roslyn from NuGet by Richard Bayler Levaro.

This package compiles and runs fine with Visual Studio CTP6. When I upgraded references to Microsoft.CodeAnalysis to the latest version I discovered that the following fails to compile as MetaFileReferences seems to have gone.

Richard has the following code to create a collection of Referenced Assemblies in the Project in which the source code will be parsed (I think). Here is his code:

Assembly csharpDisplay = typeof(CodeWalker).Assembly;
renderer.MetadataReferences.Add(
                           new MetadataFileReference(assembly.Location));

csharpDisplay.GetReferencedAssemblies()
    .ToList()
    .ForEach(a => renderer.MetadataReferences.Add(
    new MetadataFileReference(Assembly.Load(a).Location)));

He then uses the list of referenced assemblies to create CSharpCompilation as below:

 Compilation compilation = CSharpCompilation.Create("CoreRenderer",
                syntaxTrees: new List<SyntaxTree> { SyntaxTree },
               references: MetadataReferences);

 SemanticModel = compilation.GetSemanticModel(SyntaxTree);  

My research suggests that I need to amend the above to use MetadataReference.CreateFromAssembly. But I don't know how. How do I rewrite above code?

Also CSharpKind property seems to have gone for a SyntaxNode!!

Upvotes: 0

Views: 1412

Answers (1)

Joginder S Nahil
Joginder S Nahil

Reputation: 837

I contacted Richard Bayler Levaro and he provided the answer below:

        Assembly csharpDisplay = typeof(CodeWalker).Assembly;
        renderer.MetadataReferences.Add(MetadataReference.CreateFromAssembly(assembly));     
        csharpDisplay.GetReferencedAssemblies()
                     .ToList()
                     .ForEach(a => renderer.MetadataReferences.Add(MetadataReference.CreateFromAssembly(Assembly.Load(a))));

Upvotes: 1

Related Questions