国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#-通過自定義注解反射生成SQL語句[轉]

2019-11-14 13:40:54
字體:
來源:轉載
供稿:網友

轉自http://blog.163.com/jong_cai/blog/static/87028045200902033553581/

 

----------------------------------------PRogram.cs----------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
//反射命名空間
using System.Reflection;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student(1, "Jong_Cai", 21);
            Insert(stu);
        }

        public static void Insert(Object obj)
        {
            String fileds = null;
            String values = null;

            Type type = obj.GetType();
            //獲取類名
            String className = type.Name;
            //獲取所有公有屬性
            PropertyInfo[] info = type.GetProperties();

            foreach (PropertyInfo var in info)
            {
                //取得屬性的特性標簽,false表示不獲取因為繼承而得到的標簽
                Object[] attr = var.GetCustomAttributes(false);
                if (attr.Length > 0)
                {
                    //從注解數組中取第一個注解(一個屬性可以包含多個注解)
                    MyAttribute myattr = attr[0] as MyAttribute;
                    if (myattr.PrimaryKey == true)
                    {
                        continue;
                    }
                }
                fileds += var.Name + ",";
                values += "'" + var.GetValue(obj, null) + "',";
            }

            fileds = fileds.Substring(0, fileds.Length - 1);
            values = values.Substring(0, values.Length - 1);
            String sql = "insert into {0}({1}) values({2})";
            sql = String.Format(sql, className, fileds, values);
            Console.WriteLine(sql);
        }
    }
}

-----------------------------------------------MyAttribute.cs---------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    //自定義注解類
    class MyAttribute: Attribute
    {
        public Boolean PrimaryKey = false;
        public String Type = null;
    }
}

-------------------------------------------Student.cs--------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    public class Student
    {
        private int _id;
        [My(PrimaryKey = true, Type = "自動增長")] //自定義注解
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private String _name;
        [My(PrimaryKey = false, Type = "名字")] //自定義注解
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _age;
        [My(PrimaryKey = false, Type = "年齡")] //自定義注解
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public Student(int id, String name, int age)
        {
            this._id = id;
            this._name = name;
            this._age = age;
        }
    }
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西畴县| 五指山市| 福州市| 平泉县| 康保县| 南投县| 海宁市| 凤城市| 新野县| 陇南市| 吉木乃县| 修武县| 石楼县| 聊城市| 武平县| 五峰| 乃东县| 苏州市| 本溪市| 蚌埠市| 萝北县| 宁城县| 大理市| 桃江县| 石渠县| 霍邱县| 边坝县| 平陆县| 桦南县| 郸城县| 兰州市| 嘉义县| 阿勒泰市| 河间市| 剑川县| 重庆市| 仪陇县| 浦城县| 同德县| 张家口市| 游戏|