⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datafieldnotdoubleattribute.cs

📁 一个小型的ORM框架,写得不好请多多指教
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace MiniORMAttribute
{
    public class DataFieldNotDoubleAttribute : Attribute
    {
        //public DataFieldNotDoubleAttribute()
        //{ }
    }
}
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; }
    }
}

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

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

static 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]).FieldName);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -