我是靠谱客的博主 愤怒小兔子,最近开发中收集的这篇文章主要介绍.net对反射(Type)的应用实现遍历类的属性和值并实现简单的增删改查模板,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

作用反射可以通过程序操作实体类的属性值,对实体类遍历输出更改

1.反射的基本属性和方法的用法


Type t = typeof(T);//获取T类的反射
T model=new T();
t.GetProperties();获取类的属性集合
t.Name//获取类名
t.GetField("id");//获取字段
t.GetProperty("ID")//获取指定属性
t.GetProperty("ID").GetValue(model, null)//获取指定属性实体类model的值
t.GetProperty("ID").PropertyType == typeof(string)//判断指定属性类型
t.GetProperty("ID").SetValue(model, dr[item.ColumnName])//在model中为指定属性赋值

2.简单模板的书写


public class ICustomBaseDAL<T> :IBaseDAL<T>
where T : new()
{
//重构返回model的方法
public T Result(DataTable dt, Type t, DataRow dr)
{
T model = new T();
foreach (DataColumn item in dt.Columns)
{
//获取中获值t.GetProperty(item.ColumnName)如果包韩则不为空
PropertyInfo proper = t.GetProperty(item.ColumnName);
if (proper != null)
{
//如果为空给model付null
if (dr[item.ColumnName].ToString()=="")
{
proper.SetValue(model,null);
continue;
}
//将值赋给model
proper.SetValue(model, dr[item.ColumnName]);//设置值第一个参数设置到的实体类第二个设置的值
}
}
return model;
}
//判断是为可用类型过滤不可用属性
public bool GetWhere(Type t, PropertyInfo pope)
{
//以Extend结尾跳出循环
if (pope.Name.EndsWith("Extend"))//自己扩展的属性不参与查询
{
return false;
}
//将能判断的都加进去
Type[] types = {typeof(string), typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int), typeof(double), typeof(double?), typeof(decimal), typeof(decimal?) };
if (types.Contains(t))
{
return true;
}
return false;
}
public virtual int Update(T model)//根据model修改必须有id
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
List<string> fields = new List<string>();
List<string> values = new List<string>();
string tableName = t.Name;//获取类的名称
PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id
string idValue = pID.GetValue(model, null).ToString();//获取属性id的值//model是指从实体对象中获取
foreach (PropertyInfo pope in popes)
{
string proName = pope.Name;
//pope.PropertyType获取属性类型
bool whereOk = GetWhere(pope.PropertyType, pope);
if (!whereOk)
{
continue;
}
if (proName == "ID")
{
continue;
}
object val = pope.GetValue(model, null);
//if (val == null)
//{
//
continue;
//}
if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?))
{
values.Add(proName + "='" + val + "'");
}
else
{
values.Add(proName + "=" + val + "");
}
}
string value = string.Join(",", values.ToArray());
string sql = " update
[" + tableName + "]
set " + value + " where id= " + idValue;
return SqlHelper.Curtsql(sql);
}
public virtual int Add(T model)//添加一条数据
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
List<string> fields = new List<string>();
List<string> values = new List<string>();
string tableName = t.Name;//获取类的名称
//PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id
//string idValue = pID.GetValue(model, null).ToString();//获取属性id的值
foreach (PropertyInfo pope in popes)
{
string proName = pope.Name;
//pope.PropertyType获取属性类型
bool whereOk = GetWhere(pope.PropertyType,pope);
if (!whereOk)
{
continue;
}
if (proName == "ID")
{
continue;
}
object val = pope.GetValue(model, null);
if (val != null)
{
fields.Add(proName);
if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?))
{
values.Add("'" + val + "'");
}
else
{
values.Add("" + val + "");
}
}
}
string field = string.Join(",", fields.ToArray());
string value = string.Join(",", values.ToArray());
string sql = "insert into
[" + tableName + "]
(" + field + ") values(" + value + ") ";
return SqlHelper.Curtsql(sql);
}
public virtual int Delete(int id)//更具id删除
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
//
List<string> fields = new List<string>();
//
List<string> values = new List<string>();
string tableName = t.Name;//获取类的名称
//
PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id
//string idValue = pID.GetValue(model, null).ToString();//获取属性id的值
string sql = " delete from [" + tableName + "] where id=" + id;
return SqlHelper.Curtsql(sql);
}
public virtual T Search(int id)//根据id查询
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
string filter = "*";
string tableName = "[" + t.Name + "]";//获取类的名称
string where = " and id=" + id;
DataTable dt = SqlHelper.Search(filter, tableName, where);
DataRow dr = dt.Rows[0];
T model = Result(dt, t, dr);
return model;
}
public string GetWhereModel(T model)//够造where条件
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
StringBuilder values = new StringBuilder();
if (model != null)
{
foreach (PropertyInfo pope in popes)
{
string proName = pope.Name;
//pope.PropertyType获取属性类型
bool whereOk = GetWhere(pope.PropertyType,pope);
if (!whereOk)
{
continue;
}
object val = pope.GetValue(model, null);
if (val == null)
{
continue;
}
if (pope.PropertyType == typeof(int) || pope.PropertyType == typeof(int?))
{
if (val.ToString() == "0" || val.ToString() == "-1")
{
continue;
}
}
if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?))
{
values.Append(" and " + proName + " like '%" + val + "%'");
}
else
{
values.Append(" and " + proName + "=" + val + "");
}
}
}
return
values.ToString();
}
public virtual List<T> DataByPager(T model, int pageSize, int pageIndex, bool isAsc, out int count)//分页查询
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
string where = GetWhereModel(model);
List<T> list = new List<T>();
QueryBy query = new QueryBy();
query.Table = "[" + t.Name + "]";
query.Filter = "*";
query.Where = where;
query.sortColumn = "ID";
query.IsAsc = isAsc;
query.PageSize = pageSize;
query.PageIndex = pageIndex;
DataTable dt = CreateDateByPager.dataByPager(query);
count = query.Count;
foreach (DataRow dr in dt.Rows)
{
T _model = Result(dt, t, dr);
list.Add(_model);
}
return list;
}
//根据where条件查询内部构造where语句查询所有字段
public virtual List<T> Search(T model)
{
Type t = typeof(T);
List<T> list = new List<T>();
string filter = "*";
string tableName = "[" + t.Name + "]";//获取类的名称
string where = GetWhereModel(model);
DataTable dt = SqlHelper.Search(filter, tableName, where);
foreach (DataRow dr in dt.Rows)
{
T _model = Result(dt, t, dr);
list.Add(_model);
}
return list;
}
//根据指定条件修改
public int Update(T model, string[] propertyNames)
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
List<string> fields = new List<string>();
List<string> values = new List<string>();
string tableName = t.Name;//获取类的名称
PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id
string idValue = pID.GetValue(model, null).ToString();//获取属性id的值//model是指从实体对象中获取
foreach (PropertyInfo pope in popes)
{
string proName = pope.Name;
bool whereOk = GetWhere(pope.PropertyType,pope);
if (!whereOk)
{
continue;
}
if (proName == "ID")
{
continue;
}
object val = pope.GetValue(model, null);
//if (val == null)
//{
//
continue;
//}
//判断是否包含修改的元素
if (propertyNames.Contains(proName))
{
if (pope.PropertyType == typeof(string) || pope.PropertyType == typeof(DateTime) || pope.PropertyType == typeof(DateTime?))
{
values.Add(proName + "='" + val + "'");
}
else
{
values.Add(proName + "=" + val + "");
}
}
}
string value = string.Join(",", values.ToArray());
string sql = " update
[" + tableName + "]
set " + value + " where id= " + idValue;
return SqlHelper.Curtsql(sql);
}
//实现批量删除
public int Delete(string ids)
{
Type t = typeof(T);
PropertyInfo[] popes = t.GetProperties();
//
List<string> fields = new List<string>();
//
List<string> values = new List<string>();
string tableName = t.Name;//获取类的名称
//
PropertyInfo pID = t.GetProperty("ID");//获取类对应的Id
//string idValue = pID.GetValue(model, null).ToString();//获取属性id的值
string sql = " delete from [" + tableName + "] where id in ("+ids+")";
return SqlHelper.Curtsql(sql);
}
}

最后

以上就是愤怒小兔子为你收集整理的.net对反射(Type)的应用实现遍历类的属性和值并实现简单的增删改查模板的全部内容,希望文章能够帮你解决.net对反射(Type)的应用实现遍历类的属性和值并实现简单的增删改查模板所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部