本文整理汇总了C#中Microsoft.VisualBasic.VBCodeProvider.GenerateCodeFromCompileUnit方法的典型用法代码示例。如果您正苦于以下问题:C# VBCodeProvider.GenerateCodeFromCompileUnit方法的具体用法?C# VBCodeProvider.GenerateCodeFromCompileUnit怎么用?C# VBCodeProvider.GenerateCodeFromCompileUnit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.VisualBasic.VBCodeProvider
的用法示例。
在下文中一共展示了VBCodeProvider.GenerateCodeFromCompileUnit方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
internal static TemplateCompilationResult Compile(
Type templateType,
string templateBody,
IEnumerable<string> assemblyFileNames,
IEnumerable<string> namespaces,
string tempDirectory)
{
LoadRuntimeBinder();
string className;
var compileUnit = GetCodeCompileUnit(templateType, namespaces, templateBody, out className);
string sourceCode;
CodeDomProvider codeProvider;
switch (Language)
{
case TemplateCompilationLanguage.CSharp:
codeProvider = new CSharpCodeProvider();
break;
case TemplateCompilationLanguage.VisualBasic:
codeProvider = new VBCodeProvider();
break;
default:
throw new NotSupportedException("Language not supported.");
}
var builder = new StringBuilder();
using (var writer = new StringWriter(builder, CultureInfo.InvariantCulture))
{
codeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, new CodeGeneratorOptions());
sourceCode = builder.ToString();
}
var parameters = CreateCompilerParameters(tempDirectory, assemblyFileNames);
var compileResult = codeProvider.CompileAssemblyFromDom(parameters, compileUnit);
if (compileResult.Errors != null && compileResult.Errors.Count > 0)
throw new TemplateCompilationException(compileResult.Errors, sourceCode, templateBody);
var fullClassName = TEMPLATES_NAMESPACE + "." + className;
return new TemplateCompilationResult
{
Type = compileResult.CompiledAssembly.GetType(fullClassName),
SourceCode = sourceCode
};
}
示例2: GenerateVBTo
/// <summary>
/// Generates VB text from a compile unit and writes it to a text writer with the given options.
/// </summary>
/// <param name="compileUnit">The compile unit to generate text from.</param>
/// <param name="writer">The text writer to write to.</param>
/// <param name="options">The generation options.</param>
public static void GenerateVBTo(this CodeCompileUnit compileUnit, TextWriter writer, CodeGeneratorOptions options)
{
using (var provider = new VBCodeProvider())
{
provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
}
}
示例3: Main
//.........这里部分代码省略.........
CurrentlyExecutingAssemblyClass currentlyExecutingAssemblyClass=new CurrentlyExecutingAssemblyClass(""Hello World"");
Console.WriteLine(currentlyExecutingAssemblyClass.CurrentlyExecutingProperty);
"
};
codeMemberMethod.Statements.Add(codeSnippetExpression);
codeTypeDeclaration.Members.Add(codeMemberMethod);
#endregion
#region Create Region for Class
codeRegionDirectiveStart = new CodeRegionDirective(CodeRegionMode.Start, "Start MyType");
codeRegionDirectiveEnd = new CodeRegionDirective(CodeRegionMode.End, "Start MyType");
codeTypeDeclaration.StartDirectives.Add(codeRegionDirectiveStart);
codeTypeDeclaration.EndDirectives.Add(codeRegionDirectiveEnd);
#endregion
#region render code
string sourceCS = string.Empty;
StringBuilder stringBuilder = new StringBuilder();
CodeDomProvider codeDomProvider = null;
//Bracing style Block->same line C->new line
//GenerateCodeFromCompileUnit reserves the right to rearrange your code the way it knows best
CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions()
{
BracingStyle = "C",
VerbatimOrder = false,
BlankLinesBetweenMembers = true,
ElseOnClosing = false
};
//in c#
using (StringWriter stringWriter = new StringWriter())
{
codeDomProvider = new CSharpCodeProvider();
codeDomProvider.GenerateCodeFromCompileUnit(codeCompileUnit,
stringWriter,
codeGeneratorOptions);
sourceCS = stringWriter.ToString();
}
//in visualbasic
string sourceVB = string.Empty;
using (StringWriter stringWriter = new StringWriter())
{
codeDomProvider = new VBCodeProvider();
codeDomProvider.GenerateCodeFromCompileUnit(codeCompileUnit,
stringWriter,
codeGeneratorOptions);
sourceVB = stringWriter.ToString();
}
//output source
//Console.WriteLine(sourceCS);
//Console.ReadLine();
//Console.WriteLine(sourceVB);
//Console.ReadLine();
#endregion
#region declare compiler objects
CompilerParameters compilerParameters = null;
CompilerResults compilerResults = null;
int lineNumber = 0;
#endregion
#region compile CS source
CSharpCodeProvider csharpCodeProvider = null;