コード例 #1
0
        public override void GenerateCode(CompilerCSharp code)
        {
            List <MetadataReference> referencias = new List <MetadataReference>();

            var assemblys = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var item in assemblys)
            {
                try
                {
                    referencias.Add(MetadataReference.CreateFromFile(item.Location));
                }
                catch
                {
                }
            }

            foreach (string s in code.Reference)
            {
                if (s != "")
                {
                    referencias.Add(MetadataReference.CreateFromFile(s));
                }
            }

            var assemblyName = Guid.NewGuid().ToString();

            List <SyntaxTree> tree = new List <SyntaxTree>();

            switch (code.SourceType)
            {
            case SourceTypeEnum.File:
            {
                break;
            }

            case SourceTypeEnum.Text:
            {
                tree.Add(CSharpSyntaxTree.ParseText(code.Source));
                break;
            }
            }

            var cc = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: tree,
                references: referencias,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                );

            using (MemoryStream ms = new MemoryStream())
            {
                ((CompilerCSharpRoslyn)code).emitResult = cc.Emit(ms);
                ms.Position           = 0;
                code.CompiledAssembly = DllLoader.LoadDll(ms).assembly;
            }
        }
コード例 #2
0
        public override void GenerateCode(CompilerCSharp code)
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters cp       = new CompilerParameters();

            cp.GenerateExecutable      = false;
            cp.GenerateInMemory        = true;
            cp.IncludeDebugInformation = true;

            foreach (string s in code.Reference)
            {
                if (s != "")
                {
                    cp.ReferencedAssemblies.Add(s);
                }
            }

            switch (code.SourceType)
            {
            case SourceTypeEnum.File:
            {
                ((CompilerCSharpNetCore)code).compilerResults = provider.CompileAssemblyFromFile(cp, code.Source);
                break;
            }

            case SourceTypeEnum.Text:
            {
                ((CompilerCSharpNetCore)code).compilerResults = provider.CompileAssemblyFromSource(cp, code.Source);
                break;
            }
            }

            if (((CompilerCSharpNetCore)code).compilerResults.Errors.HasErrors == true)
            {
                var ex = new ExceptionCompiler();
                ex.CompilerResults = ((CompilerCSharpNetCore)code).compilerResults;
                throw ex;
            }

            code.CompiledAssembly = ((CompilerCSharpNetCore)code).compilerResults.CompiledAssembly;
        }
コード例 #3
0
 public virtual void GenerateCode(CompilerCSharp code)
 {
 }