Exemplo n.º 1
0
        public static byte[] BuidStreamAssembly(string filesrcasstr, string destfilename, int targetFramework, string type = "dll", CompilationProfile compprofile = CompilationProfile.Generic)
        {
            CSharpCompilation compilation = CreateCompilation(filesrcasstr, destfilename, GetReferences(targetFramework), type, compprofile);

            try
            {
                MemoryStream s = new MemoryStream();

                var result = compilation.Emit(s);

                if (result.Success)
                {
                    //Console.WriteLine("[*] Bulid finished IL for assembly {0} created", destfilename);
                    return(s.ToArray());
                }
                else
                {
                    Console.WriteLine("[X] Failed");
                    foreach (Diagnostic d in result.Diagnostics)
                    {
                        Console.WriteLine("[X] Debug info {0}", d.GetMessage());
                    }
                    return(null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("[x] Build error {0}", e.Message);
                Console.WriteLine(e.StackTrace);
                return(null);
            }
        }
Exemplo n.º 2
0
        static CSharpCompilation CreateCompilation(string filesrcasstr, string destfilename, IEnumerable <MetadataReference> references, string type = "dll", CompilationProfile compprofile = CompilationProfile.Generic)
        {
            var               source           = filesrcasstr;
            string            keyfilename      = Path.Combine(Directory.GetCurrentDirectory(), WORKSPACE_FOLDER, KEYFILE_FOLDER, "key.snk");
            List <SyntaxTree> compilationTrees = new List <SyntaxTree>();

            string[] sourceDirectorys = { srcPath, srcExternalPath };

            foreach (string s in sourceDirectorys)
            {
                List <SourceSyntaxTree> sourceSyntaxTrees = new List <SourceSyntaxTree>();
                sourceSyntaxTrees.AddRange(Directory.GetFiles(s, "*.cs", SearchOption.AllDirectories)
                                           .Where(F => compilationProfiles.GetValueOrDefault(compprofile).Contains(F.Substring(F.LastIndexOf(Path.DirectorySeparatorChar) + 1)))
                                           .Select(F => new SourceSyntaxTree {
                    FileName = F, SyntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(F), new CSharpParseOptions())
                })
                                           .ToList());

                compilationTrees.AddRange(sourceSyntaxTrees.Select(S => S.SyntaxTree).ToList());
            }

            SyntaxTree sourceTree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions());

            compilationTrees.Add(sourceTree);

            CSharpCompilationOptions options = null;

            if (type.Equals("exe"))
            {
                options = new CSharpCompilationOptions(outputKind: OutputKind.ConsoleApplication, optimizationLevel: OptimizationLevel.Release, platform: Platform.AnyCpu, allowUnsafe: true);
            }
            else
            {
                options = new CSharpCompilationOptions(outputKind: OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release, platform: Platform.AnyCpu, allowUnsafe: true);
            }

            CSharpCompilation compilation = null;

            if (!string.IsNullOrEmpty(type))
            {
                compilation = CSharpCompilation.Create(destfilename, compilationTrees, references, options);
            }
            else
            {
                Console.WriteLine("[x] Build error");
            }

            return(compilation);
        }