本文整理汇总了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);
}
}
}
示例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;
}
}
示例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)));
}
}
}
示例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);
}
}