dimanche 25 octobre 2020

Can't load assembly that isn't explicitly used in my project

I need to load these 4 assemblies on the fly:

"Microsoft.CodeAnalysis",
"Microsoft.CodeAnalysis.CSharp",
"Microsoft.CodeAnalysis.Features",
"Microsoft.CodeAnalysis.CSharp.Features"

They all come from nuget packages referenced in a project separate from the startup project.

But when I try loading them like this:

Assembly.Load("Microsoft.CodeAnalysis"),
Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
Assembly.Load("Microsoft.CodeAnalysis.Features"),
Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features")
// this doesn't work either:
// Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features, Version=3.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

I get a FileNotFoundException on the Assembly.Load(Microsoft.CodeAnalysis.CSharp.Features) call.

However, loading the dll directly from the packages directory like so:

Assembly.LoadFile(@"D:\project\packages\Microsoft.CodeAnalysis.CSharp.Features.3.9.0-2.20525.2\lib\netstandard2.0\Microsoft.CodeAnalysis.CSharp.Features.dll")

works perfectly fine.

I've also noticed that the Microsoft.CodeAnalysis.CSharp.Features.dll isn't copied to the startup project bin directory, while the three other dlls are. I suspect that it's because I'm not explicitly using that assembly in my own code, it's just loaded using reflection and then immediately sent to external code.

My complete intended usage/implementation looks like this

public static Document CreateDocument(string assemblyName, IEnumerable<PortableExecutableReference> referensMetadata, string documentName = "Script", 
            IEnumerable<Assembly> hostedAssemblies = null)
{
    // To prevent "The language 'C#' is not supported." exception
    var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions);

    var mefHostRequiredAssemblies = new List<Assembly>
    {
        Assembly.Load("Microsoft.CodeAnalysis"),
        Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
        Assembly.Load("Microsoft.CodeAnalysis.Features"),
        Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features")
    };

    if (hostedAssemblies != null)
    {
        mefHostRequiredAssemblies.AddRange(hostedAssemblies);
    }

    var partTypes = MefHostServices.DefaultAssemblies.Concat(mefHostRequiredAssemblies)
        .Distinct()
        .SelectMany(x => x.GetTypes())
        .ToArray();

    var compositionContext = new ContainerConfiguration()
        .WithParts(partTypes)
        .CreateContainer();

    var workspace = new AdhocWorkspace(MefHostServices.Create(compositionContext));

    var project = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(),
        assemblyName, assemblyName, LanguageNames.CSharp)
        .WithMetadataReferences(referensMetadata);

    var documentId = DocumentId.CreateNewId(project.Id);
    var documentInfo = DocumentInfo.Create(documentId, documentName,
            loader: TextLoader.From(
                TextAndVersion.Create(
                    SourceText.From(string.Empty), VersionStamp.Create())
                )
            );


    return workspace.CurrentSolution.AddProject(project)
        .AddDocument(documentInfo)
        .GetDocument(documentId);
}

My question is what am I doing wrong? How come I can't load references I've explicitly added to the project?





Aucun commentaire:

Enregistrer un commentaire