当前位置:首页 > Windows程序 > 正文

C#反射机制(转自Binfire博客)

2021-03-26 Windows程序

  [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;

            }

        }

 

        //描述一些特殊的数据库类型

        public DbType DbType

        {

            get { return _dbType; }

            set { _dbType = value; }

        }

 

    }

 

    public class OrmHelp

    {

        public void Insert(object table)

        {

            Type type = table.GetType();

            //定义一个字典来存放表中字段和值的对应序列

            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)

                {

                    //获得相应属性的值

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67688.html