Beispiel #1
0
        private SvgDocument CreateDocument(string userCode)
        {
            var source = $@"using System;
using System.Drawing;
using System.IO;
using Svg;

class Program
{{
    public static SvgDocument CreateDocument()
    {{
        SvgDocument svgDoc = null;
        {userCode}
        return svgDoc;
    }}
}}
";

            var sourcePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()?.Location ?? string.Empty);
            var sourceText = SourceText.From(source, Encoding.UTF8);

            var options = CSharpParseOptions.Default
                          .WithLanguageVersion(LanguageVersion.CSharp8);

            var syntaxTree = CSharpSyntaxTree.ParseText(
                sourceText,
                options
                )
                             .WithFilePath(Path.Combine(sourcePath, "Program.cs"));

            var runtimeDirectoryPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
            var references           = new MetadataReference[]
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(Path.Combine(runtimeDirectoryPath, "mscorlib.dll")),
                MetadataReference.CreateFromFile(Path.Combine(runtimeDirectoryPath, "System.Runtime.dll")),
#if NETFRAMEWORK
                MetadataReference.CreateFromFile(Path.Combine(runtimeDirectoryPath, "System.Drawing.dll")),
                MetadataReference.CreateFromFile(Path.Combine(runtimeDirectoryPath, "System.Xml.dll")),
#else
                MetadataReference.CreateFromFile(Path.Combine(runtimeDirectoryPath, "System.Drawing.Primitives.dll")),
                MetadataReference.CreateFromFile(Path.Combine(runtimeDirectoryPath, "System.Private.Xml.dll")),
                MetadataReference.CreateFromFile(Path.Combine(runtimeDirectoryPath, "System.Xml.ReaderWriter.dll")),
#endif
                MetadataReference.CreateFromFile(Path.Combine(sourcePath, "Svg.dll")),
            };

            var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                                     .WithOptimizationLevel(OptimizationLevel.Release);

            var compilation = CSharpCompilation.Create(
                "ConsoleApp",
                new[] { syntaxTree },
                references,
                compilationOptions);

            using (var stream = new MemoryStream())
            {
                var emitResult = compilation.Emit(stream);
                if (!emitResult.Success)
                {
                    var messages = new StringBuilder();
                    foreach (var diagnostic in emitResult.Diagnostics)
                    {
                        var pos      = diagnostic.Location.GetLineSpan();
                        var location = $"({pos.StartLinePosition.Line + 1 - 10},{pos.StartLinePosition.Character + 1})";
                        messages.AppendLine($"{location}: {diagnostic.Severity} {diagnostic.Id}: {diagnostic.GetMessage()}");
                    }
                    throw new InvalidProgramException(messages.ToString());
                }

#if !NETFRAMEWORK
                var assemblyLoadContext = new System.Runtime.Loader.AssemblyLoadContext(compilation.AssemblyName, true);
#endif
                try
                {
                    stream.Seek(0, SeekOrigin.Begin);
#if NETFRAMEWORK
                    var assembly = System.Reflection.Assembly.Load(stream.ToArray());
#else
                    var assembly = assemblyLoadContext.LoadFromStream(stream);
#endif

                    var type   = assembly.GetType("Program");
                    var method = type?.GetMethod("CreateDocument");
                    return(method?.Invoke(null, null) as SvgDocument);
                }
                catch (Exception ex)
                {
                    throw ex.InnerException ?? ex;
                }
#if !NETFRAMEWORK
                finally
                {
                    assemblyLoadContext.Unload();
                }
#endif
            }
        }
Beispiel #2
0
 /// <inheritdoc />
 public IAssembly LoadFromStream(Stream assembly)
 {
     return(Assembly.ToIAssembly(
                assemblyLoadContext.LoadFromStream(assembly)
                ));
 }