stringtemplate .net 动态对象

发布于 2024-08-24 10:38:30 字数 560 浏览 5 评论 0原文

我正在使用字符串模板来呈现一些内容,但内容可能是可变的,因此不确定如何传递它(使用.net / c#)

基本想法是我有一个 List>需要最终作为参数,例如

List<KeyValuePair<string, object>> ret = new List<KeyValuePair<string, object>>();
ret.Add(new KeyValuePair<string, object>("elem1", true));
ret.Add(new KeyValuePair(string, object>("elem2", false));

现在我希望这些在字符串模板中显示为:

$item.elem1$ $item.elem2$

我可以让它们成为 $elem1$ 或 $elem2$ 但我需要它们在结构内部。因此,我实际上需要说服字符串模板 setAttribute 我正在传递具有属性 elem1 和 elem2 的对象,而实际上我有一个 KeyValuePairs 列表。

谢谢

I am using string template to render some content, but the content may be variable so not sure how to pass it in (using .net / c#)

Basic idea is I have a List> which need to end up as parameters, e.g.

List<KeyValuePair<string, object>> ret = new List<KeyValuePair<string, object>>();
ret.Add(new KeyValuePair<string, object>("elem1", true));
ret.Add(new KeyValuePair(string, object>("elem2", false));

Now I want these to show up in string template as:

$item.elem1$
$item.elem2$

I can get them to be $elem1$ or $elem2$ but i need them inside of a structure. So I in effect need to convince the string template setAttribute that I'm passing in an object with properties elem1 and elem2 when in fact I have a List of KeyValuePairs.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-08-31 10:38:30

实际上,对其进行很小的重写就应该可以工作。您需要使用字典,甚至可以嵌套它们(使用 ST 3.2):

[Test]
public void When_Building_Text_With_A_Dictionary_As_The_Attributes_It_Should_Map_Members_To_Keys()
{
    IDictionary<string, object> ret = new Dictionary<string, object>();
    ret["elem1"] = true;
    ret["elem2"] = false;

    var nestedObj = new Dictionary<string, object>();
    nestedObj["nestedProp"] = 100;
    ret["elem3"] = nestedObj;

    var template = new StringTemplate("$elem1$ or $elem2$ and value: $elem3.nestedProp$");
    template.Attributes = ret;

    StringBuilder sb = new StringBuilder();
    StringWriter writer = new StringWriter(sb);
    template.Write(new NoIndentWriter(writer));
    writer.Flush();

    var renderedText = sb.ToString();

    Assert.That(renderedText, Is.EqualTo("True or False and value: 100"));
}

我自己和一位同事正在寻求复制 STST(ST 独立工具)的功能,它使用 json 作为属性,我们构建了一个简单的 JObject对于字典转换器,我可以发布该代码和示例(如果它对您有用的话),它只有大约 20 行。

Actually a very small re-write of that should work. You need to use a dictionary, and you can even nest them (using ST 3.2):

[Test]
public void When_Building_Text_With_A_Dictionary_As_The_Attributes_It_Should_Map_Members_To_Keys()
{
    IDictionary<string, object> ret = new Dictionary<string, object>();
    ret["elem1"] = true;
    ret["elem2"] = false;

    var nestedObj = new Dictionary<string, object>();
    nestedObj["nestedProp"] = 100;
    ret["elem3"] = nestedObj;

    var template = new StringTemplate("$elem1$ or $elem2$ and value: $elem3.nestedProp$");
    template.Attributes = ret;

    StringBuilder sb = new StringBuilder();
    StringWriter writer = new StringWriter(sb);
    template.Write(new NoIndentWriter(writer));
    writer.Flush();

    var renderedText = sb.ToString();

    Assert.That(renderedText, Is.EqualTo("True or False and value: 100"));
}

Myself and a colleague were looking to replicate the functionality of STST (ST Standalone Tool), which uses json as the properties, and we built a simple JObject to dictionary converter, I can post that code and an example if it's of any use to you, it's only ~20 lines.

女皇必胜 2024-08-31 10:38:30

ExpandoObject 的成员可以动态添加和删除运行时间。

ExpandoObject's members can be dynamically added and removed at run time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文