本文整理汇总了C#中CodeGenerator.GenerateCode方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenerator.GenerateCode方法的具体用法?C# CodeGenerator.GenerateCode怎么用?C# CodeGenerator.GenerateCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator.GenerateCode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompileTemplate
public override TemplateInfo CompileTemplate(string templateBody, Dictionary<string, Type> globalsTypes, List<Assembly> referencedAssemblies)
{
lock (_templateInfoCacheLock)
{
// Generate template program
TemplateInfo ti = GenerateTemplateProgram(templateBody, globalsTypes, referencedAssemblies);
// Generated template found in cache
if (_templateInfoCache.ContainsKey(ti.TemplateKey))
{
return _templateInfoCache[ti.TemplateKey];
}
// Generate code
ICodeGenerator codeGenerator = new CodeGenerator();
ti.GeneratedSourceCode = codeGenerator.GenerateCode(ti);
// Generate assembly
var assemblyCompiler = new AssemblyGenerator();
Assembly assembly = assemblyCompiler.GenerateAssembly(ti, true, null, null);
// Try to load the Render() method from assembly
ti.RenderMethod = GetTemplateRenderMethod(assembly, ti);
// Try to load the template generator version from assembly
ti.GeneratorVersion = GetTemplateGeneratorVersion(assembly, ti);
_templateInfoCache.Add(ti.TemplateKey, ti);
return ti;
}
}
示例2: CompileTemplate
public override TemplateInfo CompileTemplate(string templateBody, Dictionary<string, Type> globalsTypes, List<Assembly> referencedAssemblies)
{
lock (_templateInfoCacheLock)
{
// Cache is empty, load templates from cache folder
if (_templateInfoCache == null)
{
_templateInfoCache = LoadTemplatesInfo(_templateCacheFolder);
}
// Generate template program
TemplateInfo ti = GenerateTemplateProgram(templateBody, globalsTypes, referencedAssemblies);
// Generated template found in cache
if (_templateInfoCache.ContainsKey(ti.TemplateKey))
{
return _templateInfoCache[ti.TemplateKey];
}
// Generate code
ICodeGenerator codeGenerator = new CodeGenerator();
ti.GeneratedSourceCode = codeGenerator.GenerateCode(ti);
// Path to output assembly
string assemblyFileName = _fileNamePattern.Replace("{key}", ti.TemplateKey);
string assemblyPath = Path.Combine(_templateCacheFolder, assemblyFileName);
// Generate assembly
var assemblyCompiler = new AssemblyGenerator();
Assembly assembly = assemblyCompiler.GenerateAssembly(ti, false, assemblyPath, null);
// Try to load the Render() method from assembly
ti.RenderMethod = GetTemplateRenderMethod(assembly, ti);
// Try to load the template generator version from assembly
ti.GeneratorVersion = GetTemplateGeneratorVersion(assembly, ti);
_templateInfoCache.Add(ti.TemplateKey, ti);
return ti;
}
}