在实际做网站项目中,经常会遇到一些配置信息存储及修改问题。在这里将结合实例,介绍配置内容存储至WEBCONFIG文件的使用方法,要点有灵活的设计对象属性,XML文件序列化及反序列化,反射GetProperties()等。
下面我来看一张图片(页面的截图),如下:
下面来看,为这样一个页面而设计的一个对象,主要在属性上。代码如下:
public class Attr : System.Attribute { /// <summary> /// 名称 /// </summary> public string Name { get; set; } /// <summary> /// 介绍 /// </summary> public string Intro { get; set; } /// <summary> /// 帮助 /// </summary> public string Info { get; set; } /// <summary> ///文本框 /// </summary> public ControlType ControlType { get; set; } /// <summary> /// 自定义html /// </summary> public string Html { get; set; } public Attr(string name,string intro,string info,ControlType t) { Name = name; Intro = intro; Info = info; ControlType = t; } public Attr(string name, string info, ControlType t) { Name = name; Intro = ""; Info = info; ControlType = t; } }
看下对它序列化(ConfigInfo.cs)代码:
/// <summary>
/// 网站名称
/// </summary>
[Attr("网站名称","如新浪、网易","gvfgffuyguy",ControlType.Input)]
public string SiteStatic_Name { get; set; }
/// <summary>
/// 网站地址
/// </summary>
[Attr("网站地址", "例如:http://www.163.com", ControlType.Input)]
public string SiteStatic_Url { get; set; }
这个类中还有一个实例化话的方法:
/// <summary>
/// 实例化一个新的对象
/// </summary>
/// <returns></returns>
public static ConfigInfo Instance()
{
if (System.Web.HttpContext.Current.Cache[path] != null)
{
return System.Web.HttpContext.Current.Cache[path] as ConfigInfo;
}
else
{
ConfigInfo cfg;
try
{
cfg = Common.SerializationHelper.Load(typeof(ConfigInfo), path) as ConfigInfo;
}
catch (Exception)
{
cfg = new ConfigInfo();
SaveConfig(cfg);
}
if (cfg == null) cfg = new ConfigInfo();
System.Web.HttpContext.Current.Cache.Add(path, cfg, new System.Web.Caching.CacheDependency(path, System.DateTime.Now), System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
return cfg;
}
}
这个方法中还调用了SerializationHelper.cs文件中的Load()方法XML序列化。
在页面加载显示时,用以下代码。
/// <summary>
/// 绑定默认数据
/// </summary>
private void BindDefault()
{
System.Type tp = typeof(Com.Config.ConfigInfo);
System.Text.StringBuilder strb = new System.Text.StringBuilder();
string temp = string.Empty;
strb.AppendFormat("<form method=\"post\" id=\"tf\" action=\"\"><div title=\"{0}\"><table ><tr> <td colspan=\"2\"></td></tr>" + System.Environment.NewLine, "基本配置", (new System.Random()).Next(30));
foreach (PropertyInfo pro in tp.GetProperties())
{
Com.Config.Attr[] attrs = pro.GetCustomAttributes(typeof(Com.Config.Attr), true) as Com.Config.Attr[];
Com.Config.Attr attr = attrs.Length == 1 ? attrs[0] : null;
if (attr != null)
{
switch (attr.ControlType)
{
case Com.Config.ControlType.Input:
temp = "<input name=\"{0}\" type=\"text\" size=\"30\" style=\"width:300px;\" value='{3}'/>";
break;
}
strb.AppendFormat(" <tr><td >{0}:</td><td >{1}</td></tr>" + System.Environment.NewLine, attr.Name,
string.Format(temp + "{2}{1}", pro.Name,
string.IsNullOrEmpty(attr.Info) ? "" : string.Format(" <img src='/images/icons/icon34.jpg' width='18' style='vertical-align:bottom' title='{0}' name='tip'/>", attr.Info),
string.IsNullOrEmpty(attr.Intro) ? "" : (" " + attr.Intro), pro.GetValue(infoData, null)));
}
}
strb.Append("<tr><td colspan='2'k><button type=\"submit\" class=\"ManagerButton\"\"><img src=\"../images/submit.gif\"/> 保 存 </button></td></tr>");
strb.Append("</table></div></form>");
Infos = strb.ToString();
}
好了,到这里就介绍完了。欢迎大家指正,本DEMO源码下载地址:https://files.cnblogs.com/howzanh/BaseConfig.zip