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();
        }
    }

  • 相关阅读:
    关于sharepoint组里面有哪些成员
    如何在ProjectServer用代码修改用户属性?
    Project Server的Psi汇总
    Sharepoint Two Webpart Connect
    TQ6410_V3 wince6.0系统 调试口改普通串口方法
    流方式文件读写(简单实现)
    Django 框架请求响应流程图
    Silverlight项目基本文件结构
    Ext.DomHelper类的使用示例(内容操作)
    利用urllib2获取请求头部信息
  • 原文地址:https://www.cnblogs.com/shiningrise/p/1422694.html
Copyright © 2011-2022 走看看