本文整理汇总了C#中Microsoft.CSharp.CSharpCodeProvider.CompileAsync方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCodeProvider.CompileAsync方法的具体用法?C# CSharpCodeProvider.CompileAsync怎么用?C# CSharpCodeProvider.CompileAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CSharp.CSharpCodeProvider
的用法示例。
在下文中一共展示了CSharpCodeProvider.CompileAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
internal static async Task<LiveCodeCompilationResult> Compile(IEnumerable<string> sourceFilesPaths, string projectName, IEnumerable<string> references, bool createAssemblyFiles = false)
{
string[] filesPaths = sourceFilesPaths as string[] ?? sourceFilesPaths.ToArray();
string[] sourceFiles = filesPaths.ToArray();
string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string assemblyName = GetNewAssemblyName(projectName, location);
// clean up previous assemblies
DeletePreviousAssemblies(projectName, location);
// setup compilation parameters
var options = SetupCompilationParameters(createAssemblyFiles, assemblyName, sourceFiles, location, references);
// compile
var cSharpCodeProvider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
CompilerResults results = await cSharpCodeProvider.CompileAsync(options, sourceFiles);
cSharpCodeProvider.Dispose();
var errorMessage = new StringBuilder();
if (results.Errors.HasErrors)
{
errorMessage.Append(string.Format("Compilation errors for {0}", projectName));
foreach (CompilerError error in results.Errors)
{
errorMessage.Append(string.Format("{0}({1},{2}): {3}", error.FileName, error.Line, error.Column, error.ErrorText));
}
return new LiveCodeCompilationResult{ ErrorMessage = errorMessage.ToString() };
}
// load compiled assembly
if (createAssemblyFiles)
{
// load from disk
return await LoadAssemblyFromDisk(assemblyName);
}
// load from memory
return new LiveCodeCompilationResult { Assembly = results.CompiledAssembly, HasSucceed = true };
}