zoukankan      html  css  js  c++  java
  • C#

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

  • 相关阅读:
    全排列和全组合实现
    (原)关于MEPG-2中的TS流数据格式学习
    linux的PAM认证和shadow文件中密码的加密方式
    vim 撤销 回退操作
    memcached解压报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now的解决方法
    Linux系统安全之pam后门安装使用详解
    漏洞预警:Linux内核9年高龄的“脏牛”0day漏洞
    linux软链接的创建、删除和更新
    关于“.bash_profile”和“.bashrc”区别的总结
    linux下批量杀死进程
  • 原文地址:https://www.cnblogs.com/coder-axin/p/5044621.html
Copyright © 2011-2022 走看看