zoukankan      html  css  js  c++  java
  • 反射与ORM的例子

    using System;
    using System.Collections;

    using System.Reflection;

    public class Person
    {
        private string _Name;
        private int _Age;
        private string _Sex;

        public string Name
        {
            get { return this._Name; }
            set { this._Name = value; }
        }

        public int Age
        {
            get { return this._Age; }
            set { this._Age = value; }
        }

        public string Sex
        {
            get { return this._Sex; }
            set { this._Sex = value; }
        }
    }
    //测试代码如下:

     

    class Program
    {
        [STAThread]
        static void Main()
        {
            Person person = new Person();
            person.Name = "snoopy";
            person.Age = 5;
            person.Sex = "male";

            PropertyInfo[] infos = person.GetType().GetProperties();
            Console.WriteLine("打印属性");
            foreach (PropertyInfo info in infos)
            {
                //获取属性并打印
                Console.WriteLine(info.Name + ":" + info.GetValue(person, null));
            }

            Console.WriteLine("设置Person.Name = Hellokitty");
            //设置属性,设置Name属性
            foreach (PropertyInfo info in infos)
            {
                if (info.Name == "Name")
                {
                    info.SetValue(person, "Hellokitty", null);
                }
            }

            Console.WriteLine("打印属性");
            foreach (PropertyInfo info in infos)
            {
                //获取属性并打印
                Console.WriteLine(info.Name + ":" + info.GetValue(person, null));
            }
            Console.Read();
        }
    }

    ////////////////////////////////////////////////////////////////////////

    using System;
    using System.Collections;

    using System.Reflection;

    public class DataFieldAttribute : Attribute
    {
        private string _FieldName;
        private string _FieldType;

        public DataFieldAttribute(string fieldname, string fieldtype)
        {
            this._FieldName = fieldname;
            this._FieldType = fieldtype;
        }

        public string FieldName
        {
            get { return this._FieldName; }
            set { this._FieldName = value; }
        }

        public string FieldType
        {
            get { return this._FieldType; }
            set { this._FieldType = value; }
        }
    }

    public class Person
    {
        private string _Name;
        private int _Age;
        private string _Sex;

        [DataFieldAttribute("name", "nvarchar")]
        public string Name
        {
            get { return this._Name; }
            set { this._Name = value; }
        }

        [DataFieldAttribute("age", "int")]
        public int Age
        {
            get { return this._Age; }
            set { this._Age = value; }
        }

        [DataFieldAttribute("sex", "nvarchar")]
        public string Sex
        {
            get { return this._Sex; }
            set { this._Sex = value; }
        }
    }


    class Program
    {
        [STAThread]
        static void Main()
        {
            Person person = new Person();
            person.Name = "snoopy";
            person.Age = 5;
            person.Sex = "male";

            PropertyInfo[] infos = person.GetType().GetProperties();

            object[] objDataFieldAttribute = null;
            foreach (PropertyInfo info in infos)
            {
                objDataFieldAttribute = info.GetCustomAttributes(typeof(DataFieldAttribute), false);
                if (objDataFieldAttribute != null)
                {
                    Console.WriteLine(info.Name + "->数据库字段:" + ((DataFieldAttribute)objDataFieldAttribute[0]).FieldType);
                }
            }
      
      System.Console.Read();
        }
    }

  • 相关阅读:
    Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
    ROS_Kinetic_19 群机器人框架示例(micros swarm framework)
    ROS_Kinetic_18 使用V-Rep3.3.1和Matlab2015b(vrep_ros_bridge)续
    ROS_Kinetic_17 使用V-Rep3.3.1(vrep_ros_bridge)
    USB OTG原理+ ID 检测原理
    高通QSD MSM APQ区别
    qualcomm memory dump 抓取方法
    msm8974 camera driver添加新摄像头kernel hal修改
    现代控制理论-章节组织结构和仿真应用案例详细分析
    ROS_Kinetic_16 ubuntu中安装使用Matlab和ROS
  • 原文地址:https://www.cnblogs.com/shiningrise/p/1422694.html
Copyright © 2011-2022 走看看