Andrus wrote:
I have signature templates containing variable parts like user name
between << and >signs, like
Best regards,
<<User.FirstName><<User.FirstName>>
MyCompany
Baker street 123
Phone: <<User.Phone>>
How to replace fields between << and > marks with current values in
runtime ?
Create a method that returns the value when you send it the key of a
variable:
private static string GetValue(string key) {
switch (key) {
case "User.FirstName": return "Göran";
case "User.LastName": return "Andersson";
case "User.Phone": return "012-34 56 78";
default: throw new ArgumentOutOfRangeException("key", key, "Unknown
key.");
}
}
Then you can use that with a regular expression that replaces the variables:
string message = Regex.Replace(template, "<<([^>]+)>>", delegate(Match
m) { return GetValue(m.Groups[1].Value); });
Is it possible to evaluate expressions also if << >contains expressions ?
Everything is possible, but that is quite a bit trickier. What kind of
expressions were you thinking of?
--
Göran Andersson
_____
http://www.guffa.com