I try to accomplish using a Nuget Package in dynamic compiled code using Roslyn Compiler.
I want to use a Nuget Package ( in my example https://www.nuget.org/packages/TinyCsvParser/ ) within my code. So I downloaded the package and extracted it to a folder (C:\Data\packages\tinycsvparser.2.6.1)
But I don't want it to be in my applications direct dependencies. So it is not referenced in the project itself and I don't want to copy it to the bin/Debug-Folder.
The Nuget-Package-DLL should be able to be anywhere on my harddisk.
The compilation runs without any errors. But on Runtime on the line method.Invoke(fooInstance, null) I get the following Exception:
Could not load file or assembly 'TinyCsvParser, Version=2.6.1.0, Culture=neutral, PublicKeyToken=d7df35b038077099' or one of its dependencies. The system cannot find the file specified.
How can I tell the programm where it should look for the DLL? I tried it with the following line
Assembly nugetAssembly = Assembly.LoadFrom(pathToNugetDLL);
But that did not help.
Thank you for any advice on how to resolve this issue.
Here is my Code (Prototype):
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
namespace Playground
{
public static class Program
{
private const string pathToNugetDLL = @"C:\Data\packages\tinycsvparser.2.6.1\lib\net45\TinyCsvParser.dll";
private const string firstClass =
@"
using TinyCsvParser;
namespace A
{
public class MyClass
{
public int MyFunction()
{
CsvParserOptions csvParserOptions = new CsvParserOptions(true, ';');
return 1;
}
}
}";
public static void Main()
{
CSharpParseOptions parseOptions = new CSharpParseOptions(LanguageVersion.CSharp7, DocumentationMode.Parse, SourceCodeKind.Regular);
SyntaxTree parsedSyntaxTree = SyntaxFactory.ParseSyntaxTree(firstClass, parseOptions);
List<string> defaultNamespaces = new List<string>() { };
//// Referenzen über Kommentare heraussuchen:
List<MetadataReference> defaultReferences = CreateMetadataReferences();
var encoding = Encoding.UTF8;
var assemblyName = Path.GetRandomFileName();
var symbolsName = Path.ChangeExtension(assemblyName, "pdb");
var sourceCodePath = "generated.cs";
var buffer = encoding.GetBytes(firstClass);
var sourceText = SourceText.From(buffer, buffer.Length, encoding, canBeEmbedded: true);
var syntaxTree = CSharpSyntaxTree.ParseText(
sourceText,
new CSharpParseOptions(),
path: sourceCodePath);
var syntaxRootNode = syntaxTree.GetRoot() as CSharpSyntaxNode;
var encoded = CSharpSyntaxTree.Create(syntaxRootNode, null, sourceCodePath, encoding);
CSharpCompilationOptions defaultCompilationOptions =
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithOverflowChecks(true).WithOptimizationLevel(OptimizationLevel.Debug).WithPlatform(Platform.AnyCpu)
.WithUsings(defaultNamespaces);
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
syntaxTrees: new[] { encoded },
references: defaultReferences,
options: defaultCompilationOptions
);
using (var assemblyStream = new MemoryStream())
using (var symbolsStream = new MemoryStream())
{
var emitOptions = new EmitOptions(
debugInformationFormat: DebugInformationFormat.Pdb,
pdbFilePath: symbolsName);
var embeddedTexts = new List<EmbeddedText> { EmbeddedText.FromSource(sourceCodePath, sourceText) };
EmitResult result = compilation.Emit(
peStream: assemblyStream,
pdbStream: symbolsStream,
embeddedTexts: embeddedTexts,
options: emitOptions);
if (result.Success)
{
Console.WriteLine("Complation succeeded!");
try
{
Assembly nugetAssembly = Assembly.LoadFrom(pathToNugetDLL);
var assembly = Assembly.Load(assemblyStream.ToArray(), symbolsStream.ToArray());
var type = assembly.GetType("A.MyClass");
MethodInfo method = type.GetMethod("MyFunction");
var fooInstance = Activator.CreateInstance(type);
method.Invoke(fooInstance, null);
}
catch (Exception ex)
{
int i = 0;
}
}
}
}
private static List<MetadataReference> CreateMetadataReferences()
{
string defaultPath = typeof(object).Assembly.Location.Replace("mscorlib", "{0}");
var metadatenReferences = new List<MetadataReference>()
{
MetadataReference.CreateFromFile(string.Format(defaultPath, "mscorlib")),
MetadataReference.CreateFromFile(string.Format(defaultPath, "System")),
MetadataReference.CreateFromFile(string.Format(defaultPath, "System.Data")),
MetadataReference.CreateFromFile(string.Format(defaultPath, "System.Core")),
MetadataReference.CreateFromFile(string.Format(defaultPath, "System.ComponentModel.DataAnnotations")),
MetadataReference.CreateFromFile(string.Format(defaultPath, "System.Xml")),
MetadataReference.CreateFromFile(string.Format(defaultPath, "netstandard")),
};
string strExtraDll = pathToNugetDLL;
metadatenReferences.Add(MetadataReference.CreateFromFile(strExtraDll));
return metadatenReferences;
}
}
}