Exemple #1
0
        private CompilationResult ToResult(
            Tuple <FSharpErrorInfo[], int, FSharpOption <Assembly> > compilation)
        {
            if (compilation.Item3 == null || compilation.Item3.Equals(FSharpOption <Assembly> .None))
            {
                return(CompilationResult.Failed(
                           compilation
                           .Item1
                           .Select(errorInfo => errorInfo.ToString())));
            }

            return(CompilationResult.Complete(compilation.Item3.Value));
        }
        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())));
            }
        }