属性及反射GetProperties()的一些用法,这里结合网站实例来介绍下

在实际做网站项目中,经常会遇到一些配置信息存储及修改问题。在这里将结合实例,介绍配置内容存储至WEBCONFIG文件的使用方法,要点有灵活的设计对象属性,XML文件序列化及反序列化,反射GetProperties()等。

下面我来看一张图片(页面的截图),如下:

2011012813273583.jpg

下面来看,为这样一个页面而设计的一个对象,主要在属性上。代码如下:

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("&nbsp;<img src='/images/icons/icon34.jpg' width='18' style='vertical-align:bottom' title='{0}' name='tip'/>", attr.Info),
                        string.IsNullOrEmpty(attr.Intro) ? "" : ("&nbsp;" + 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源码下载地址:http://files.cnblogs.com/howzanh/BaseConfig.zip

posted on 2011-01-30 21:14 一路->向前 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/howzanh/archive/2011/01/30/GetProperties.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值