当前位置: 首页>>代码示例>>C#>>正文


C# Compilation.Emit方法代码示例

本文整理汇总了C#中Compilation.Emit方法的典型用法代码示例。如果您正苦于以下问题:C# Compilation.Emit方法的具体用法?C# Compilation.Emit怎么用?C# Compilation.Emit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Compilation的用法示例。


在下文中一共展示了Compilation.Emit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetAssemblyFromCompilation

        public static CompilationResult GetAssemblyFromCompilation(
            IAssemblyLoadContext loader,
            Compilation compilation)
        {
            EmitResult result;
            using (var ms = new MemoryStream())
            {
                using (var pdb = new MemoryStream())
                {
                    if (PlatformHelper.IsMono)
                    {
                        result = compilation.Emit(ms, pdbStream: null);
                    }
                    else
                    {
                        result = compilation.Emit(ms, pdbStream: pdb);
                    }

                    if (!result.Success)
                    {
                        var formatter = new DiagnosticFormatter();
                        var errorMessages = result.Diagnostics
                                             .Where(IsError)
                                             .Select(d => formatter.Format(d));

                        return CompilationResult.FromErrorMessages(errorMessages);
                    }

                    ms.Seek(0, SeekOrigin.Begin);

                    Assembly assembly;
                    if (PlatformHelper.IsMono)
                    {
                        assembly = loader.LoadStream(ms, assemblySymbols: null);
                    }
                    else
                    {
                        pdb.Seek(0, SeekOrigin.Begin);
                        assembly = loader.LoadStream(ms, pdb);
                    }

                    return CompilationResult.FromAssembly(assembly);
                }
            }
        }
开发者ID:jamiuaz,项目名称:Scaffolding,代码行数:45,代码来源:CommonUtilities.cs

示例2: GetTokenToLocationMap

 public static string GetTokenToLocationMap(Compilation compilation, bool maskToken = false)
 {
     using (var exebits = new MemoryStream())
     {
         using (var pdbbits = new MemoryStream())
         {
             compilation.Emit(exebits, pdbbits);
             return Token2SourceLineExporter.TokenToSourceMap2Xml(pdbbits, maskToken);
         }
     }
 }
开发者ID:RoryVL,项目名称:roslyn,代码行数:11,代码来源:PdbTestUtilities.cs

示例3: MakeDll

 private static byte[] MakeDll(Compilation compilation)
 {
     using (var ms = new MemoryStream()) {
         var result = compilation.Emit(ms);
         if (!result.Success) {
             return null;
         }
         ms.Seek(0, SeekOrigin.Begin);
         return ms.ToArray();
     }
 }
开发者ID:andrecarlucci,项目名称:tddanalyzer,代码行数:11,代码来源:Compiler.cs

示例4: emitAndLoad

 public static Type emitAndLoad(Compilation compilation, ITypeSymbol cur)
 {
     System.Reflection.Assembly assembly;
     if (compilationAssembly_cache.Keys.Contains(compilation)) assembly = compilationAssembly_cache[compilation];
     else {
         var name = "emittedOne1.dll";
         var filePath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), name);
         if (!SetOfEmmitedCompilations.Contains(compilation))
         {
             compilation.Emit(filePath);
             SetOfEmmitedCompilations.Add(compilation);
         }
         assembly = System.Reflection.Assembly.LoadFrom(filePath);
         compilationAssembly_cache[compilation] = assembly;
     }
     return assembly.GetTypes().Where(x => x.Name == cur.Name).First();
 }
开发者ID:Emettant,项目名称:Metr2,代码行数:17,代码来源:MetrCore.cs

示例5: Execute

        // A simple helper to execute the code present inside a compilation.
        public string Execute(Compilation comp)
        {
            var output = new StringBuilder();
            string exeFilename = "OutputCS.exe", pdbFilename = "OutputCS.pdb", xmlCommentsFilename = "OutputCS.xml";
            EmitResult emitResult = null;

            using (var ilStream = new FileStream(exeFilename, FileMode.OpenOrCreate))
            {
                using (var pdbStream = new FileStream(pdbFilename, FileMode.OpenOrCreate))
                {
                    using (var xmlCommentsStream = new FileStream(xmlCommentsFilename, FileMode.OpenOrCreate))
                    {
                        // Emit IL, PDB and xml documentation comments for the compilation to disk.
                        emitResult = comp.Emit(ilStream, pdbStream, xmlCommentsStream);
                    }
                }
            }

            if (emitResult.Success)
            {
                var p = Process.Start(
                    new ProcessStartInfo()
                    {
                        FileName = exeFilename,
                        UseShellExecute = false,
                        RedirectStandardOutput = true
                    });
                output.Append(p.StandardOutput.ReadToEnd());
                p.WaitForExit();
            }
            else
            {
                output.AppendLine("Errors:");
                foreach (var diag in emitResult.Diagnostics)
                {
                    output.AppendLine(diag.ToString());
                }
            }

            return output.ToString();
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:42,代码来源:FAQ.cs

示例6: GetGroupedImportStrings

        private static ImmutableArray<ImmutableArray<string>> GetGroupedImportStrings(Compilation compilation, string methodName, out ImmutableArray<string> externAliasStrings)
        {
            Assert.NotNull(compilation);
            Assert.NotNull(methodName);

            using (var exebits = new MemoryStream())
            {
                using (var pdbbits = new MemoryStream())
                {
                    compilation.Emit(exebits, pdbbits);

                    exebits.Position = 0;
                    using (var module = new PEModule(new PEReader(exebits, PEStreamOptions.LeaveOpen), metadataOpt: IntPtr.Zero, metadataSizeOpt: 0))
                    {
                        var metadataReader = module.MetadataReader;
                        MethodDefinitionHandle methodHandle = metadataReader.MethodDefinitions.Single(mh => metadataReader.GetString(metadataReader.GetMethodDefinition(mh).Name) == methodName);
                        int methodToken = metadataReader.GetToken(methodHandle);

                        pdbbits.Position = 0;
                        ISymUnmanagedReader reader = (ISymUnmanagedReader)TempPdbReader.CreateUnmanagedReader(pdbbits);

                        return reader.GetCSharpGroupedImportStrings(methodToken, methodVersion: 1, externAliasStrings: out externAliasStrings);
                    }
                }
            }
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:26,代码来源:UsingDebugInfoTests.cs

示例7: GenerateIntermediateAssembly

        private bool GenerateIntermediateAssembly(Compilation compilation, ref Assembly assembly)
        {
            using (var stream = new MemoryStream())
            {
                var emission = compilation.Emit(stream);
                if (!emission.Success)
                {
                    foreach (var diagnostic in emission.Diagnostics)
                    {
                        if (diagnostic.Severity == DiagnosticSeverity.Error)
                            BuildEngine.LogError(diagnostic.GetMessage(Thread.CurrentThread.CurrentUICulture));
                    }
                    return false;
                }

                stream.Position = 0;
                assembly = Assembly.Load(stream.GetBuffer());
            }

            return true;
        }
开发者ID:taori,项目名称:DynamicCodeGenerator,代码行数:21,代码来源:DynamicFileGeneratorTask.cs

示例8: GetGroupedImportStrings

        private static ImmutableArray<ImmutableArray<string>> GetGroupedImportStrings(Compilation compilation, string methodName, out ImmutableArray<string> externAliasStrings)
        {
            Assert.NotNull(compilation);
            Assert.NotNull(methodName);

            using (var exebits = new MemoryStream())
            {
                using (var pdbbits = new MemoryStream())
                {
                    compilation.Emit(exebits, pdbbits);

                    exebits.Position = 0;
                    using (var metadata = ModuleMetadata.CreateFromStream(exebits, leaveOpen: true))
                    {
                        var module = metadata.Module;
                        var metadataReader = module.MetadataReader;
                        MethodDefinitionHandle methodHandle = metadataReader.MethodDefinitions.Single(mh => metadataReader.GetString(metadataReader.GetMethodDefinition(mh).Name) == methodName);
                        int methodToken = metadataReader.GetToken(methodHandle);

                        // Create a SymReader, rather than a raw COM object, because
                        // SymReader implements ISymUnmanagedReader3 and the COM object
                        // might not.
                        pdbbits.Position = 0;
                        var reader = SymReaderFactory.CreateReader(pdbbits);
                        return reader.GetCSharpGroupedImportStrings(methodToken, methodVersion: 1, externAliasStrings: out externAliasStrings);
                    }
                }
            }
        }
开发者ID:Wazner,项目名称:roslyn,代码行数:29,代码来源:UsingDebugInfoTests.cs

示例9: GetPdbXml

        internal static string GetPdbXml(Compilation compilation, string qualifiedMethodName = "")
        {
            string actual = null;
            using (var exebits = new MemoryStream())
            {
                using (var pdbbits = new MemoryStream())
                {
                    compilation.Emit(
                        exebits,
                        pdbbits,
                        options: EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.Pdb));

                    pdbbits.Position = 0;
                    exebits.Position = 0;

                    actual = PdbToXmlConverter.ToXml(pdbbits, exebits, PdbToXmlOptions.ResolveTokens | PdbToXmlOptions.ThrowOnError, methodName: qualifiedMethodName);
                }

                ValidateDebugDirectory(exebits, compilation.AssemblyName + ".pdb");
            }

            return actual;
        }
开发者ID:daking2014,项目名称:roslyn,代码行数:23,代码来源:SharedCompilationUtils.cs

示例10: CompileToDisk

        private AssemblyLoadResult CompileToDisk(string assemblyPath, string pdbPath, Compilation compilation, IList<ResourceDescription> resources)
        {
            #if DESKTOP
            EmitResult result = compilation.Emit(assemblyPath, pdbPath, manifestResources: resources);
            #else
            EmitResult result = compilation.Emit(assemblyPath);
            #endif

            if (!result.Success)
            {
                // REVIEW: Emit seems to create the output assembly even if the build fails
                // follow up to see why this happens
                if (File.Exists(assemblyPath))
                {
                    File.Delete(assemblyPath);
                }

                return ReportCompilationError(result);
            }

            return new AssemblyLoadResult(Assembly.LoadFile(assemblyPath));
        }
开发者ID:Gidra,项目名称:KRuntime,代码行数:22,代码来源:RoslynAssemblyLoader.cs

示例11: CompileInMemory

        private AssemblyLoadResult CompileInMemory(string name, Compilation compilation, IEnumerable<ResourceDescription> resources)
        {
            using (var pdbStream = new MemoryStream())
            using (var assemblyStream = new MemoryStream())
            {
            #if DESKTOP
                EmitResult result = compilation.Emit(assemblyStream, pdbStream: pdbStream, manifestResources: resources);
            #else
                EmitResult result = compilation.Emit(assemblyStream);
            #endif

                if (!result.Success)
                {
                    return ReportCompilationError(result);
                }

                var assemblyBytes = assemblyStream.ToArray();

            #if DESKTOP
                var pdbBytes = pdbStream.ToArray();
            #endif

                var compiled = new CompiledAssembly
                {
            #if DESKTOP
                    Assembly = Assembly.Load(assemblyBytes, pdbBytes),
            #else
                    Assembly = Assembly.Load(assemblyBytes),
            #endif
                    MetadataReference = compilation.ToMetadataReference()
                };

                _compiledAssemblies[name] = compiled;

                return new AssemblyLoadResult(compiled.Assembly);
            }
        }
开发者ID:Gidra,项目名称:KRuntime,代码行数:37,代码来源:RoslynAssemblyLoader.cs

示例12: DumpCompilation

 /// <summary>
 /// Dump generated compilation binary to dump directory for further use.
 /// </summary>
 /// <param name="compilation"></param>
 /// <returns></returns>
 private string DumpCompilation(Compilation compilation)
 {
     var dump = CompilationDumpPath(dumpSeq++);
     using (var stream = new FileStream(dump, FileMode.CreateNew))
     {
         compilation.Emit(stream);
         return dump;
     }
 }
开发者ID:jthelin,项目名称:SparkCLR,代码行数:14,代码来源:RoslynScriptEngine.cs

示例13: EmitCompilation

        internal static bool EmitCompilation(
            Compilation compilation,
            IEnumerable<ResourceDescription> manifestResources,
            List<ModuleData> dependencies,
            DiagnosticBag diagnostics,
            CompilationTestData testData,
            out ImmutableArray<byte> assembly,
            out ImmutableArray<byte> pdb
        )
        {
            assembly = default(ImmutableArray<byte>);
            pdb = default(ImmutableArray<byte>);

            EmitReferences(compilation, dependencies, diagnostics);

            using (var executableStream = new MemoryStream())
            {
                MemoryStream pdbStream = new MemoryStream();

                EmitResult result;
                try
                {
                    result = compilation.Emit(
                        executableStream,
                        pdbStream: pdbStream,
                        xmlDocumentationStream: null,
                        win32Resources: null,
                        manifestResources: manifestResources,
                        options: EmitOptions.Default,
                        testData: testData,
                        cancellationToken: default(CancellationToken));
                }
                finally
                {
                    if (pdbStream != null)
                    {
                        pdb = pdbStream.ToImmutable();
                        pdbStream.Dispose();
                    }
                }

                diagnostics.AddRange(result.Diagnostics);
                assembly = executableStream.ToImmutable();

                return result.Success;
            }
        }
开发者ID:JinGuoGe,项目名称:roslyn,代码行数:47,代码来源:HostedRuntimeEnvironment.cs

示例14: GetPdbXml

        internal static string GetPdbXml(Compilation compilation, string qualifiedMethodName = "")
        {
            string actual = null;
            using (var exebits = new MemoryStream())
            {
                using (var pdbbits = new MemoryStream())
                {
                    compilation.Emit(exebits, pdbbits);

                    pdbbits.Position = 0;
                    exebits.Position = 0;

                    actual = PdbToXmlConverter.ToXml(pdbbits, exebits, PdbToXmlOptions.ResolveTokens | PdbToXmlOptions.ThrowOnError, methodName: qualifiedMethodName);
                }
            }

            return actual;
        }
开发者ID:JinGuoGe,项目名称:roslyn,代码行数:18,代码来源:SharedCompilationUtils.cs

示例15: Emit

 static void Emit(Compilation compilation, out byte[] assembly)
 {
     using (var assemblyStream = new MemoryStream())
     {
         Check(compilation.Emit(assemblyStream));
         assembly = assemblyStream.GetBuffer();
     }
 }
开发者ID:jthelin,项目名称:Nake,代码行数:8,代码来源:Engine.cs


注:本文中的Compilation.Emit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。