wystarczy dodać do @svick Opcja pierwsza odpowiedź. Jeśli chcesz, aby utrzymać zespół w pamięci (zamiast zapisywania do pliku) można wykorzystać następujące metody:
AssemblyLoadContext context = AssemblyLoadContext.Default;
Assembly assembly = context.LoadFromStream(ms);
Inaczej jest, że w Net451 gdzie kod to:
Assembly assembly = Assembly.Load(ms.ToArray());
My Kod jest kierowany zarówno na Net451, jak i Netstandard, więc musiałem użyć dyrektyw, aby obejść ten problem. Pełny przykład kodu znajduje się tutaj:
string code = CreateFunctionCode();
var syntaxTree = CSharpSyntaxTree.ParseText(code);
MetadataReference[] references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(typeof(Hashtable).GetTypeInfo().Assembly.Location)
};
var compilation = CSharpCompilation.Create("Function.dll",
syntaxTrees: new[] { syntaxTree },
references: references,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
StringBuilder message = new StringBuilder();
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
message.AppendFormat("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
return new ReturnValue<MethodInfo>(false, "The following compile errors were encountered: " + message.ToString(), null);
}
else
{
ms.Seek(0, SeekOrigin.Begin);
#if NET451
Assembly assembly = Assembly.Load(ms.ToArray());
#else
AssemblyLoadContext context = AssemblyLoadContext.Default;
Assembly assembly = context.LoadFromStream(ms);
#endif
Type mappingFunction = assembly.GetType("Program");
_functionMethod = mappingFunction.GetMethod("CustomFunction");
_resetMethod = mappingFunction.GetMethod("Reset");
}
}
Dziękuję bardzo! – Mottor
Czy widzisz moje inne pytanie? http://stackoverflow.com/questions/37482955/how-can-i-use-net-core-class-library-netstandard-v1-5-into-net-framework-4 – Mottor
Twój przykład działa dobrze, jednak jest nie można dodać 'ReadLine' lub' WriteLine' z parametrami. Gdzie mogę uzyskać dokumentację dotyczącą przeciążenia i metod? Ponieważ jest bardzo dziwne, gdy Console ma metody Write, ale nie ma żadnej ReadLine. –