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