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

原文地址:https://www.cnblogs.com/shiningrise/p/1422694.html