在ST4/C#中,是否可以注册一个没有模板组的新渲染器?

在ST4/C#中,是否可以注册一个没有模板组的新渲染器?,c#,stringtemplate,stringtemplate-4,C#,Stringtemplate,Stringtemplate 4,RegisterRenderer方法出现的唯一位置是TemplateGroup。但是我只有一个通过字符串提供的模板,而不是文件系统上的多个模板 或者,我如何使用TemplateGroup,但通过字符串提供模板?好吧,我知道了如何从字符串创建模板组(这里的文档不太好,尤其是对于C#port)。虽然不是实际问题的解决方案,但它是一种解决方法,因为我可以向组注册自定义渲染器 本质上,我正在为单个模板创建一个模板组,这看起来很愚蠢,但不管怎样: // Create the group (I'm spec

RegisterRenderer方法出现的唯一位置是TemplateGroup。但是我只有一个通过字符串提供的模板,而不是文件系统上的多个模板


或者,我如何使用TemplateGroup,但通过字符串提供模板?

好吧,我知道了如何从字符串创建模板组(这里的文档不太好,尤其是对于C#port)。虽然不是实际问题的解决方案,但它是一种解决方法,因为我可以向组注册自定义渲染器

本质上,我正在为单个模板创建一个模板组,这看起来很愚蠢,但不管怎样:

// Create the group (I'm specifying custom delimiters, but you don't have to)
var group = new TemplateGroup('$','$');

// Here's where we bind the renderers to a type. They will run on EVERY occurrence of this type, which means you have to handle situations in your renderer where no format string was specified -- that's still gonna get run through the renderer (very important with strings, for instance)
group.RegisterRenderer(typeof(DateTime), new DateRenderer());

//Here's where you "register" (define) a template. You have to give it a name, and (weirdly) specify all the attributes you're going to use
group.DefineTemplate("default", "[template code here]", new string[] { "nameOfAttribute" });
// You can define more templates here...

// Now, get the template back out using the name you used before
var template = group.GetInstanceOf("default");

// Add the data (correctly using the names specified when you defined the template above)
template.Add("nameOfAttribute", my attribute);

// Render
var result = template.Render();
RegisterRenderer()
方法可通过
-属性获得:

Dim tmpl As New Template(s, "$", "$")
tmpl.Group.RegisterRenderer(GetType(DateTime), New MyDateRenderer())

这似乎可行,但您是否能够在模板中指定格式?在Java版本中,您似乎能够在
中传递字符串,但我尝试此操作时遇到异常:
[InvalidOperationException:Stack empty.]
我想我没有尝试。渲染器只在该类型的每个实例上运行。我想我没有尝试传递格式。没关系,我的模板中有一个错误,双引号
被转义到
。语法正确时,似乎正在传递格式字符串。