【发布时间】:2023-03-25 06:27:01
【问题描述】:
我有一个字符串。
此字符串是在运行时从文件中提取的,可以是任何格式,如下例所示。
唯一的规则是括号内的单词必须转换为dynamic 对象上的属性,并且它是向用户询问的值,可能使用winforms PropertyGrid 或ObjectListView。然后将用户的答案合并到字符串模板中(可能使用FormattableStringFactory.Create ?)。
示例字符串:
string template = "The client name is {name} and the client's age is {age}";
string template2 = "This house is on {streetName} and the door number is {doorNumber}";
对象提取:
dynamic templateObject = objectExtractor (templateString);
最终字符串(用户将数据引入templateObject后):
string result = "The client name is John and the client's age is 25";
换句话说,我想做的类似于 C# 6 编译器使用字符串插值语法糖所做的事情。
我正在寻找一种方法(最好是一种简单的通用方法),但我自己无法弄清楚,并且在进行了广泛的谷歌搜索后也没有找到它。
【问题讨论】:
标签: c# string winforms dynamic