小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C# 反射機(jī)制

 luan_it 2017-12-14
  [Orm.Table("TestORM")]
        public class TestORM
        {  
            [Orm.Colum("Id",DbType.Int32)]
            public int Id { get; set; }
            [Orm.Colum("UserName", DbType.String)]
            public string UserName { get; set; }
            [Orm.Colum("Password", DbType.String)]
            public string Password { get; set; }
            [Orm.Colum("CreatedTime", DbType.DateTime)]
            public DateTime CreatedTime { get; set; }
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            TestORM t = new TestORM()
            {
                Id=1,
                UserName="binfire",
                Password="xxx",
                CreatedTime=DateTime.Now
            };
            Orm.OrmHelp h=new Orm.OrmHelp();
            h.Insert(t);
        }
namespace Orm
{
    [AttributeUsageAttribute(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public class TableAttribute : Attribute
    {
        //保存表名的字段
        private string _tableName;
        public TableAttribute()
        {
        }
        public TableAttribute(string tableName)
        {
            this._tableName = tableName;
        }
        ///
        /// 映射的表名(表的全名:模式名.表名)
        ///
        public string TableName
        {
            set
            {
                this._tableName = value;
            }
            get
            {
                return this._tableName;
            }
        }
    }
    [AttributeUsageAttribute(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public class ColumAttribute : Attribute
    {
        private string _columName;
        private DbType _dbType;
        public ColumAttribute()
        {
        }
        public ColumAttribute(string columName)
            : this()
        {
            this._columName = columName;
        }
        public ColumAttribute(string columName, DbType dbType)
            : this(columName)
        {
            this._dbType = dbType;
        }
        //列名
        public virtual string ColumName
        {
            set
            {
                this._columName = value;
            }
            get
            {
                return this._columName;
            }
        }
        //描述一些特殊的數(shù)據(jù)庫(kù)類型
        public DbType DbType
        {
            get { return _dbType; }
            set { _dbType = value; }
        }
    }
    public class OrmHelp
    {
        public void Insert(object table)
        {
            Type type = table.GetType();
            //定義一個(gè)字典來(lái)存放表中字段和值的對(duì)應(yīng)序列
            Dictionary<string,string> columValue = new Dictionary<string,string>();
            StringBuilder SqlStr = new StringBuilder();
            SqlStr.Append("insert into ");
            //得到表名子
            TableAttribute temp = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), false).First();
            SqlStr.Append(temp.TableName);
            SqlStr.Append("(");
            PropertyInfo[] Propertys = type.GetProperties();
            foreach (var item in Propertys)
            {
                object[] attributes = item.GetCustomAttributes(false);
                foreach (var item1 in attributes)
                {
                    //獲得相應(yīng)屬性的值
                    string value = table.GetType().InvokeMember(item.Name, System.Reflection.BindingFlags.GetProperty, null, table, null).ToString();
                    ColumAttribute colum = item1 as ColumAttribute;
                    if (colum != null)
                    {
                        columValue.Add(colum.ColumName, value);
                    }
                }
            }
            //拼插入操作字符串
            foreach (var item in columValue)
            {
                SqlStr.Append(item.Key);
                SqlStr.Append(",");
            }
            SqlStr.Remove(SqlStr.Length - 1, 1);
            SqlStr.Append(") values('");
            foreach (var item in columValue)
            {
                SqlStr.Append(item.Value);
                SqlStr.Append("','");
            }
            SqlStr.Remove(SqlStr.Length - 2, 2);
            SqlStr.Append(")");
            HttpContext.Current.Response.Write(SqlStr.ToString());
        }
    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多