反射

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using CFramework.CAttribute;
using CData;

namespace CFramework.PageBase
{
    public class EntityBase
    {
        public string TableName
        {
            get
            {
                Type msgType = this.GetType();
                Type tableNameType = typeof(TableNameAttribute);
                object[] arr = msgType.GetCustomAttributes(tableNameType, true);
                TableNameAttribute tnAttribute = arr[0] as TableNameAttribute;
                return tnAttribute.TableName;
            }
        }

        public Dictionary<string, string> GetDicColumn()
        {
            Type msgType = this.GetType();
            Dictionary<string, string> dicColumn = new Dictionary<string, string>();
            Type columnNameType = typeof(ColumnNameAttribute);
            object[] arr;

            PropertyInfo[] proList = msgType.GetProperties();
            MethodInfo m;
            object rs;
            string strKey = string.Empty;
            string strValue = string.Empty;
            PropertyInfo pro;
            ColumnNameAttribute aa;
            for(int i=0;i<proList.Length ;i++)
            {
                pro = proList[i];
                //利用反射取得属性的get方法。
                m = pro.GetGetMethod();
                //利用反射调用属性的get方法,取得属性的值
                rs = m.Invoke(this, null);

                arr = pro.GetCustomAttributes(columnNameType, true);
                if (!(arr.Length > 0))
                {
                    continue;
                }
                aa = (ColumnNameAttribute)arr[0];
                strKey = aa.ColumnName;
                if (strKey.ToLower() != "id")
                {
                    dicColumn.Add("c_" + strKey, rs.ToString());
                }
                else
                {
                    Id = rs.ToString();
                }
            }
            return dicColumn;
        }

        DbHelper dbHelper = new DbHelper();
        #region 属性
 
        private string _id = "";
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
        #endregion

        public int Insert()
        {
            return dbHelper.Insert(GetDicColumn(), TableName);
        }

        public int Update()
        {
            return dbHelper.Update(GetDicColumn(), TableName, "id", Id);
        }
    }
}



#region Using
using System;
using System.Collections.Generic;
using System.Text;
using CFramework.CAttribute;
using CFramework.PageBase;
#endregion

namespace Entity
{
    ///<summary>
    ///资讯 Entity
    ///</summary>
    [Serializable]
    [TableName("News")]
 public class NewsInfo : EntityBase
 {
  public NewsInfo()
  {}
 
     #region 属性
        private int _ID;
        private string _Title;
        private string _Body;
        private string _Source;
        private string _Author;
        private DateTime _CreateDate;
        private int _TypeID;
        private string _Recommend;
        private int _ReadTimes;

        [ColumnName("ID")]
        /// <summary>
        ///
        /// </summary>
        public int ID
        {
            get{return _ID; }
            set{_ID = value;}
        }

        [ColumnName("Title")]
        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get{return _Title; }
            set{_Title = value;}
        }

        [ColumnName("Body")]
        /// <summary>
        /// 内容
        /// </summary>
        public string Body
        {
            get{return _Body; }
            set{_Body = value;}
        }

        [ColumnName("Source")]
        /// <summary>
        /// 来源
        /// </summary>
        public string Source
        {
            get{return _Source; }
            set{_Source = value;}
        }

        [ColumnName("Author")]
        /// <summary>
        /// 作者
        /// </summary>
        public string Author
        {
            get{return _Author; }
            set{_Author = value;}
        }

        [ColumnName("CreateDate")]
        /// <summary>
        /// 创建日期
        /// </summary>
        public DateTime CreateDate
        {
            get{return _CreateDate; }
            set{_CreateDate = value;}
        }

        [ColumnName("TypeID")]
        /// <summary>
        ///
        /// </summary>
        public int TypeID
        {
            get{return _TypeID; }
            set{_TypeID = value;}
        }

        [ColumnName("Recommend")]
        /// <summary>
        /// 推荐
        /// </summary>
        public string Recommend
        {
            get{return _Recommend; }
            set{_Recommend = value;}
        }

        [ColumnName("ReadTimes")]
        /// <summary>
        ///
        /// </summary>
        public int ReadTimes
        {
            get{return _ReadTimes; }
            set{_ReadTimes = value;}
        }

        #endregion
 }
}

你可能感兴趣的:(反射)