}
public string MeetingName
{
get
{
return "Productor page";
}
set
{
//
}
}
public string RoomName
{
get
{
return "24-1";
}
set
{
//
}
}
public string BeginTime
{
get
{
return "2007-12-1 12:01:29";
}
set
{
//
}
}
#endregion
public string GenerationPage()
{
//读取模板的内容(方法自己写吧)
string content = "你好,#?MemberName?#请出席会议:#?MeetingName?#。地点:#?RoomName?#。时间:#?BeginTime?#。";
StringBuilder builder = new StringBuilder(content);
string pattern = @"#\?(?'property'\S+?)\?#";
return Regex.Replace(content, pattern, new MatchEvaluator(RegexMatchEvaluation), RegexOptions.ExplicitCapture);
}
private string RegexMatchEvaluation(Match match)
{
//get the property name (named group of the regex)
string propertyName = match.Groups["property"].Value;
//try to get a property handle from the business object
PropertyInfo pi = this.GetType().GetProperty(propertyName);
//do not replace anything if no such property exists
if (pi == null)
{
return match.Value;
}
//return the property value
object propertyValue = pi.GetValue(this, null);
if (propertyValue != null)
{
return propertyValue.ToString();
}
else
{
return string.Empty;
}
}
}
比较模板内容:
你好,#?MemberName?#请出席会议:#?MeetingName?#。地点:#?RoomName?#。时间:#?BeginTime?#。
运行结果:
你好,Vodgo请出席会议:Productor page。地点:24-1。时间:2007-12-1 12:01:29。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-623595/ ,如需转载,请注明出处,否则将追究法律责任。