模板标签替换
生成静态页时.一般会用到标签替换,下面这个类是最近写来做个方便的替换使用..给过路的朋友分享一下,有用,maybe
public class FormatList
{
public bool IsTestMode { get; set; }
NameValueCollection nvc = null;
public FormatList()
{
nvc = new NameValueCollection();
}
public void Add(string name, string value)
{
if (nvc.AllKeys.Contains(name)) nvc.Set(name, value);
else nvc.Add(FormatName(name), value);
}
public void Add<FountaionEntityClass>(FountaionEntityClass source)
{
string FountaionEntityClassName = source.GetType().Name;
Add<FountaionEntityClass>(source, FountaionEntityClassName);
}
public void Add<FountaionEntityClass>(FountaionEntityClass source, string FountaionEntityClassName)
{
FountaionEntityClassName += ".";
foreach (PropertyInfo pi in source.GetType().GetProperties())
{
try
{
Add(FountaionEntityClassName + pi.Name, pi.GetValue(source, null).ToString());
}
catch (Exception ex)
{ }
}
}
private static string FormatName(string name)
{
return string.Format(@"\[{0}\]", name.Replace(".", "\\."));
}
public string Format(string input)
{
if (nvc.Count == 0)
return input;
foreach (string key in nvc.AllKeys)
{
string value = nvc[key];
if (value == null) value = "";
if (IsTestMode) input = Regex.Replace(input, key, key + ":" + value, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
else input = Regex.Replace(input, key, value, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
}
return input;
}
public static string Format(string input, string name, string text)
{
return Regex.Replace(input, FormatName(name), text, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
}
{
public bool IsTestMode { get; set; }
NameValueCollection nvc = null;
public FormatList()
{
nvc = new NameValueCollection();
}
public void Add(string name, string value)
{
if (nvc.AllKeys.Contains(name)) nvc.Set(name, value);
else nvc.Add(FormatName(name), value);
}
public void Add<FountaionEntityClass>(FountaionEntityClass source)
{
string FountaionEntityClassName = source.GetType().Name;
Add<FountaionEntityClass>(source, FountaionEntityClassName);
}
public void Add<FountaionEntityClass>(FountaionEntityClass source, string FountaionEntityClassName)
{
FountaionEntityClassName += ".";
foreach (PropertyInfo pi in source.GetType().GetProperties())
{
try
{
Add(FountaionEntityClassName + pi.Name, pi.GetValue(source, null).ToString());
}
catch (Exception ex)
{ }
}
}
private static string FormatName(string name)
{
return string.Format(@"\[{0}\]", name.Replace(".", "\\."));
}
public string Format(string input)
{
if (nvc.Count == 0)
return input;
foreach (string key in nvc.AllKeys)
{
string value = nvc[key];
if (value == null) value = "";
if (IsTestMode) input = Regex.Replace(input, key, key + ":" + value, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
else input = Regex.Replace(input, key, value, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
}
return input;
}
public static string Format(string input, string name, string text)
{
return Regex.Replace(input, FormatName(name), text, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
}
无论你添加什么标签,最终都是以String的格式对源进行replace,这里还包括一个比较好用的方法,就是用泛型+反射去增加标签,这样可以减少比较繁琐的功夫,一次性把整个类都添加进去了
通常使用的标签为 [标签名] 或 [类名.字段名]...