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


C# CSharpCompilation.Emit方法代码示例

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


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

示例1: GetAssemblyFromCompilation

        public static CompiledAssemblyResult GetAssemblyFromCompilation(
            CSharpCompilation compilation)
        {
            EmitResult result;
            using (var ms = new MemoryStream())
            {
                using (var pdb = new MemoryStream())
                {
                    if (_isMono.Value)
                    {
                        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 CompiledAssemblyResult.FromErrorMessages(errorMessages);
                    }

                    Assembly assembly;
                    if (_isMono.Value)
                    {
                        var assemblyLoadMethod = typeof(Assembly).GetTypeInfo().GetDeclaredMethods("Load")
                            .First(
                                m =>
                                    {
                                        var parameters = m.GetParameters();
                                        return parameters.Length == 1 && parameters[0].ParameterType == typeof(byte[]);
                                    });
                        assembly = (Assembly)assemblyLoadMethod.Invoke(null, new[] { ms.ToArray() });
                    }
                    else
                    {
                        var assemblyLoadMethod = typeof(Assembly).GetTypeInfo().GetDeclaredMethods("Load")
                            .First(
                                m =>
                                    {
                                        var parameters = m.GetParameters();
                                        return parameters.Length == 2
                                               && parameters[0].ParameterType == typeof(byte[])
                                               && parameters[1].ParameterType == typeof(byte[]);
                                    });
                        assembly = (Assembly)assemblyLoadMethod.Invoke(null, new[] { ms.ToArray(), pdb.ToArray() });
                    }

                    return CompiledAssemblyResult.FromAssembly(assembly);
                }
            }
        }
开发者ID:adwardliu,项目名称:EntityFramework,代码行数:57,代码来源:RoslynCompilationService.cs

示例2: CreateAssembly

        private Assembly CreateAssembly(CSharpCompilation compilation)
        {
            using (var outputStream = new MemoryStream())
            using (var pdbStream = new MemoryStream())
            {
                // Emit assembly to streams
                var result = compilation.Emit(outputStream, pdbStream);
                if (!result.Success)
                {
                    throw new CompilationException(result);
                }

                // Load the compiled assembly;
                var assembly = Assembly.Load(outputStream.ToArray(), pdbStream.ToArray());
                return assembly;
            }
        }
开发者ID:mahic,项目名称:RoslynSamples,代码行数:17,代码来源:CodeExecutor.cs

示例3: BuildAssembly

 /// <summary>
 /// Builds the assembly.
 /// </summary>
 protected virtual Assembly BuildAssembly(CSharpCompilation compilation)
 {
     using (var ms = new MemoryStream())
     {
         var result = compilation.Emit(ms);
         if (result.Success)
         {
             var assembly = Assembly.Load(ms.ToArray());
             assemblyCache.AddAssembly(assembly, compilation.ToMetadataReference());
             return assembly;
         }
         else
         {
             throw new Exception("The compilation failed! This is most probably bug in the DotVVM framework.\r\n\r\n"
                 + string.Join("\r\n", result.Diagnostics)
                 + "\r\n\r\n" + compilation.SyntaxTrees[0].GetRoot().NormalizeWhitespace() + "\r\n\r\n"
                 + "References: " + string.Join("\r\n", compilation.ReferencedAssemblyNames.Select(n => n.Name)));
         }
     }
 }
开发者ID:darilek,项目名称:dotvvm,代码行数:23,代码来源:DefaultViewCompiler.cs

示例4: Save

 private void Save()
 {
     if (Options.FullCompile)
     {
         var bindingsAssemblyPath = bindingCompiler.OutputFileName;
         bindingCompiler.SaveAssembly();
         Program.WriteInfo("bindings saved to " + bindingsAssemblyPath);
         compilation = compilation.AddReferences(MetadataReference.CreateFromFile(Path.GetFullPath(bindingsAssemblyPath)));
         var compiledViewsFileName = Path.Combine(Options.OutputPath, Options.AssemblyName + ".dll");
         //Directory.CreateDirectory("outputCS");
         //int i = 0;
         //foreach (var tree in compilation.SyntaxTrees)
         //{
         //    File.WriteAllText($"outputCS/file{i++}.cs", tree.GetRoot().NormalizeWhitespace().ToString());
         //}
         var result = compilation.Emit(compiledViewsFileName);
         if (!result.Success)
         {
             throw new Exception("compilation failed");
         }
         Program.WriteInfo("views saved to " + compiledViewsFileName);
     }
 }
开发者ID:darilek,项目名称:dotvvm,代码行数:23,代码来源:ViewStaticCompilerCompiler.cs


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