예제 #1
0
        public override CompilationResult Compile(string source)
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(source);

            var references =
                GetReferencedAssemblies(() => Regex.Matches(source, "using ([^;]+)").Cast <Match>().Select(m => m.Groups[1].Value));
            var compilation = CSharpCompilation.Create("IPlayer", new[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms);
                if (result.Success)
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    return(CompilationResult.Complete(Assembly.Load(ms.ToArray())));
                }

                return(CompilationResult.Failed(
                           result
                           .Diagnostics
                           .Select(diagnostic => diagnostic.ToString())));
            }
        }