これが私の疑似テンプレートです
Dear {User},
Your job finished at {FinishTime} and your file is available for download at {FileURL}.
Regards,
{Signature}
私はGoogleでc#でテンプレート解析を検索し、いくつかの優れたライブラリを見つけましたが、それらのライブラリは完全にc4.0バージョン用です。私はc#v2.0で作業しています。だから誰でも私にc#v2.0の文字列テンプレートを解析するための良いライブラリを提案することができます。C#2.0で文字列テンプレートを解析するための最良かつ簡単な方法について簡単に説明してください。ありがとう
私はRegExで簡単な解決策を得ました
string template = "Some @@Foo@@ text in a @@Bar@@ template";
StringDictionary data = new StringDictionary();
data.Add("foo", "random");
data.Add("bar", "regex");
string result = Regex.Replace(template, @"@@([^@]+)@@", delegate(Match match)
{
string key = match.Groups[1].Value;
return data[key];
});
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program {
static void Main() {
var template = " @@3@@ @@2@@ @@__@@ @@Test ZZ@@";
var replacement = new Dictionary<string, string> {
{"1", "Value 1"},
{"2", "Value 2"},
{"Test ZZ", "Value 3"},
};
var r = new Regex("@@(?<name>.+?)@@");
var result = r.Replace(template, m => {
var key = m.Groups["name"].Value;
string val;
if (replacement.TryGetValue(key, out val))
return val;
else
return m.Value;
});
Console.WriteLine(result);
}
}