特性给对象自动赋值

using System;
using System.Collections.Generic;
using System.Text;

namespace Reflection
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    public class RemarkAttribute : Attribute
    {
        public string Remark { get; private set; }
        public RemarkAttribute(string remark)
        {
            Remark = remark;
        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    public class DBRemarkAttribute : Attribute
    {
        public string Remark { get; private set; }
        public DBRemarkAttribute(string remark)
        {
            Remark = remark;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace Reflection
{
    public class BaseModel
    {
        public int Id { set; get; }
    }

    public class Company : BaseModel
    {
        [Remark("公司名")]
        public string CompanyName { set; get; }
    }
    [DBRemark("Users")]
    public class User : BaseModel
    {
        [Remark("用户名")]
        public string UserName { set; get; }
        [DBRemark("State")]
        public byte? Status { set; get; }
    }
}

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Data.SqlClient;

namespace Reflection
{
    public static class DBHelper
    {
        private static string ConnectionStr = @"Data Source=DESKTOP-JUCUAL1\SQLEXPRESS01;Initial Catalog=MySqlServer;User Id=sa;Password=wang";
        public static void GetProperty<T>(this T t) where T : BaseModel
        {
            //Type type = typeof(T);
            Type type = t.GetType();
            Console.WriteLine(type.Name);
            foreach (PropertyInfo property in type.GetProperties())
            {
                if (property.IsDefined(typeof(RemarkAttribute), true))
                {
                    RemarkAttribute attribute = (RemarkAttribute)property.GetCustomAttribute(typeof(RemarkAttribute), true);
                    //attribute.Remark
                    Console.WriteLine($"{ attribute.Remark}-{property.GetValue(t)}");
                }
            }
        }

        private static string GetPropDBName<T>(this T t) where T : MemberInfo
        {
            if (t.IsDefined(typeof(DBRemarkAttribute), true))
            {
                var attribute = (DBRemarkAttribute)t.GetCustomAttribute(typeof(DBRemarkAttribute), true);
                return attribute.Remark;
            }
            return t.Name;
        }

        private static Action<object, Type, SqlDataReader> action = (obj, type, sdr) =>
        {
            foreach (PropertyInfo property in type.GetProperties())
            {
                var value = sdr[property.GetPropDBName()] == DBNull.Value ? null : sdr[property.GetPropDBName()];
                property.SetValue(obj, value);
            }
        };

        public static T GetData<T>(int Id) where T : BaseModel
        {
            Type type = typeof(T);
            string sql = $"select top 1 {string.Join(',', type.GetProperties().Select(a => a.GetPropDBName()))}" +
                $" from {type.GetPropDBName()} where Id = @Id";
            object obj = Activator.CreateInstance(type);
            using (SqlConnection cn = new SqlConnection(ConnectionStr))
            {
                cn.Open();
                SqlCommand command = new SqlCommand(sql, cn);
                command.Parameters.Add(new SqlParameter("@Id", Id));
                SqlDataReader sdr = command.ExecuteReader();
                if (sdr.Read())
                {
                    action(obj, type, sdr);
                }
                else
                {
                    return null;
                }
                cn.Close();
            }
            return (T)obj;
        }

        public static List<T> GetData<T>() where T : BaseModel
        {
            Type type = typeof(T);
            string sql = $"select {string.Join(',', type.GetProperties().Select(a => a.GetPropDBName()))}" +
                $" from {type.GetPropDBName()}";
            List<T> lst = new List<T>();
            using (SqlConnection cn = new SqlConnection(ConnectionStr))
            {
                cn.Open();
                SqlCommand command = new SqlCommand(sql, cn);
                SqlDataReader sdr = command.ExecuteReader();
                while (sdr.Read())
                {
                    object obj = Activator.CreateInstance(type);
                    action(obj, type, sdr);
                    lst.Add((T)obj);
                }
                cn.Close();
            }
            return lst;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值