属性及反射GetProperties()的一些用法,我们结合网站实例介绍下:
在实际做网站项目中,经常会遇到一些配置信息存储及修改问题。在这里将结合实例,介绍配置内容存储至WEBCONFIG文件的使用方法,要点有灵活的设计对象属性,XML文件序列化及反序列化,反射GetProperties()等。
下面我们来看一张图片(页面的截图),如下:
下面来看,为这样一个页面而设计的一个对象,主要在属性上。代码如下:
01 public class Attr : System.Attribute
02 {
03
04 /// <summary>
05 /// 名称
06 /// </summary>
07 public string Name { get; set; }
08 /// <summary>
09 /// 介绍
10 /// </summary>
11 public string Intro { get; set; }
12 /// <summary>
13 /// 帮助
14 /// </summary>
15 public string Info { get; set; }
16 /// <summary>
17 ///文本框
18 /// </summary>
19 public ControlType ControlType { get; set; }
20
21 /// <summary>
22 /// 自定义html
23 /// </summary>
24 public string Html { get; set; }
25 public Attr(string name,string intro,string info,ControlType t)
26 {
27 Name = name;
28 Intro = intro;
29 Info = info;
30 ControlType = t;
31 }
32 public Attr(string name, string info, ControlType t)
33 {
34 Name = name;
35 Intro = "";
36 Info = info;
37 ControlType = t;
38 }
39 }
看下对它序列化(ConfigInfo.cs)代码:
/// <summary>
/// 网站名称
/// </summary>
[Attr("网站名称","如新浪、网易","gvfgffuyguy",ControlType.Input)]
public string SiteStatic_Name { get; set; }
/// <summary>
/// 网站地址
/// </summary>
[Attr("网站地址", "例如:", 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='http://www.jzxue.com/images/icons/icon34.jpg' title='{0}'/>", 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=http://www.jzxue.com/"../images/submit.gif\"/> 保 存 </button></td></tr>");
strb.Append("</table></div></form>");
Infos = strb.ToString();
}