本文整理汇总了C#中Compiler.CompileTemplate方法的典型用法代码示例。如果您正苦于以下问题:C# Compiler.CompileTemplate方法的具体用法?C# Compiler.CompileTemplate怎么用?C# Compiler.CompileTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Compiler
的用法示例。
在下文中一共展示了Compiler.CompileTemplate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
string templateSource = @"@using System.Diagnostics
@property int Count
@property string Name
<html>
<head>
</head>
<body>
@for (int i = 0; i < Count; i++) {
<p>Hello @Name!</p>
}
</body>
</html>";
CodeGenerator codeGenerator = new CodeGenerator();
var codeGeneratorParameters = new CodeGeneratorParameters();
codeGeneratorParameters.ClassName = "Foo";
var codeGeneratorResult = codeGenerator.Generate(templateSource, codeGeneratorParameters);
//Console.WriteLine(codeGeneratorResult.Code);
Compiler compiler = new Compiler();
var compilerParameters = new CompilerParameters();
var template = compiler.CompileTemplate(templateSource, compilerParameters);
template.SetPropertyValue("Count", 20);
template.SetPropertyValue("Name", "Bob Freeman");
Console.WriteLine(template.Run());
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
示例2: RunTest
private void RunTest(string input, string expected, Dictionary<string, object> properties, Action<Template> check)
{
CodeGenerator codeGenerator = new CodeGenerator();
string code = codeGenerator.Generate(input, new CodeGeneratorParameters()).Code;
Template template;
string actual;
try
{
Compiler compiler = new Compiler();
template = compiler.CompileTemplate(input, new CompilerParameters());
foreach (var pair in properties)
{
template.SetPropertyValue(pair.Key, pair.Value);
}
actual = template.Run();
Console.WriteLine("=== Output ===");
Console.WriteLine(actual);
}
finally
{
Console.WriteLine("=== Code ===");
Console.WriteLine(code);
}
expected = expected.Replace(Environment.NewLine, string.Empty);
actual = actual.Replace(Environment.NewLine, string.Empty);
Assert.AreEqual(expected, actual);
check(template);
}