转自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;
        }
    }
}

最新文章

  1. python之路目录
  2. 使用gulp解决外部编辑器修改Eclipse文件延迟更新的问题
  3. WordPress上传含有中文文件出现乱码
  4. oracle数据泵示例
  5. 现在写 PHP,你应该知道这些
  6. java9-4 包
  7. Java基础(49):快速排序的Java封装(含原理,完整可运行,结合VisualGo网站更好理解)
  8. PHPnow升级PHP 5.4与Mysql 5.5
  9. Redis源码研究--字典
  10. perl 安装AnyEvent::HTTP
  11. 使用 Visual Studio 对exe文件进行数字签名
  12. Mysql表结构定义及相关语法
  13. 自定义结构化config文件
  14. Centos7 zip unzip 安装和使用
  15. OpenCV+VisualStudion2017配置
  16. EasyGui 学习文档【超详细中文版】
  17. 【读书笔记】Android平台的漏洞挖掘和分析
  18. 孩子们各显神通对付 iOS 12「屏幕使用时间」的限制
  19. Vijos / 题库 / 输油管道问题
  20. Angular动态表单生成(三)

热门文章

  1. Cortex-M3学习日志(五) -- DAC实验
  2. 调试qemu
  3. cloud computing platform,virtual authentication encryption
  4. I Hate It(线段树)
  5. OracleOraDb10g_home1TNSListener服务无法启动
  6. matlab最小二乘法数据拟合函数详解
  7. IIS发布网站:CS0016: 未能写入输出文件的解决方法
  8. Cookie已经过时,细看Facebook, Google, Apple如何追踪用户
  9. jQuery给table中的负数标红色
  10. hdu 4031 Attack 线段树