数据模型类对比 反射c#
using System;
using System.ComponentModel.DataAnnotations;
public class LoginModel
{
[Display(Name = "用户名")]
public string UserName { get; set; }
public string ExternalLoginData { get; set; }
}
/// <summary>
/// 说明:对比实体类
/// 时间:2014年2月20日 14:17:48
/// Auhter:Kim
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="newModel"></param>
/// <param name="oldModel"></param>
/// <returns></returns>
public string CompareModel<T>(T newModel, T oldModel) where T : class,new ()
{
StringBuilder strLog = new StringBuilder();
System.Reflection. PropertyInfo[] mPi = typeof(T).GetProperties();
for (int i = 0; i < mPi.Length; i++)
{
System.Reflection. PropertyInfo pi = mPi[i];
string strName = string .Empty;
string log=string .Empty;
if (pi.GetValue(oldModel, null ) != null && pi.GetValue(newModel, null ) != null)
{
strName = Attributes.GetDisplayInfo<T>(pi.Name);
string oldValue = pi.GetValue(oldModel, null).ToString();
string newValue = pi.GetValue(newModel, null).ToString();
if (oldValue != newValue)
{
if (GetNameByDictonary(strName, oldValue, newValue, out log))
strLog.Append(log);
else
strLog.AppendFormat("<strong>{0}</strong><span>{0} 从 {1} 改为 {2}</span> ", strName, oldValue, newValue);
}
}
}
return strLog.ToString();
}
/// <summary>
/// 获取DisplayInfo
/// </summary>
/// <param name="t"></param>
public static string GetDisplayInfo<T>( string name) where T : class,new ()
{
Type objType = typeof (T);
// Using reflection.
string strName = string .Empty;
PropertyInfo propInfo = objType.GetProperty(name);
object[] attrs = propInfo.GetCustomAttributes(typeof (DisplayAttribute), true); // Reflection.
// Displaying output.
if (attrs.Length > 0)
{
DisplayAttribute a = attrs[0] as DisplayAttribute;
strName = a.Name;
}
return strName;
}
数据模型类对比 反射c#的更多相关文章
- phpcms v9二次开发之数据模型类
系统模型类:model.class.php数据模型类的位置:/phpcms/libs/classes phpcms v9二次开发中,我们要经常需要对模块的数据表进行查询.添加.修改和删除数据等操作,所 ...
- JAVA进阶之旅(二)——认识Class类,反射的概念,Constructor,Field,Method,反射Main方法,数组的反射和实践
JAVA进阶之旅(二)--认识Class类,反射的概念,Constructor,Field,Method,反射Main方法,数组的反射和实践 我们继续聊JAVA,这次比较有意思,那就是反射了 一.认识 ...
- C#工具:反射帮助类 泛型反射帮助类
反射帮助类 using System; using System.Reflection; using System.Data; using System.Drawing; using System.R ...
- 类的反射及xml文件的解析
类的反射 xml文件的解析 .properties||.xml配置文件的创建及读取内容 //创建对象 Properties properties = new Properties(); //存储 pr ...
- 类的反射_reflex
JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为j ...
- python基础-类的反射
1)反射是通过字符串方式映射内存中的对象. python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr, 改四个函数分别用于对对象内部执行:检查是 ...
- Django项目的ORM操作之--数据模型类创建
在django项目中,其自带了ORM(Object Relation Mapping)对象关系映射框架,我们在django项目下app的models模块下对类进行操作,通过ORM会将我们对类的操作转化 ...
- java--分析简单java类与反射的联系
分析简单java类与反射的联系 web对反射的操作支持 在JSP之中有一种技术--javaBean.而且在jsp里面也配套有相应的操作方式,javaBean的核心在于简单java类,于是下面演示此操作 ...
- 类的反射实例(servlet的抽取)
类的反射实例 具体以后我们写的时候不用写BaseServlet,因为各种框架都已经给我们写好了 所以,user对应的servlet的界面长这样:
随机推荐
- 如何用pdb进行python调试
本文章讲述了如何用pdb进行python调试讲解. 当手边 没有IDE,面对着python调试犯愁时,你就可以参考下本文;(pdb 命令调试) 参 考:http://docs.python.org/l ...
- unity3d 镜头随鼠标移动
using UnityEngine; using System.Collections; public class sheji : MonoBehaviour { public int speed = ...
- AX Dynamic 2012 SSRS 按行数分页
按行数分页 1. Create a new Row Group with the following grouping expression: =Ceiling(RowNumber(Nothing)/ ...
- input自动获取焦点
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>te ...
- Python list 操作
创建列表sample_list = ['a',1,('a','b')] Python 列表操作sample_list = ['a','b',0,1,3] 得到列表中的某一个值value_start = ...
- Spark Streaming源码解读之Job动态生成和深度思考
本期内容 : Spark Streaming Job生成深度思考 Spark Streaming Job生成源码解析 Spark Core中的Job就是一个运行的作业,就是具体做的某一件事,这里的JO ...
- Oracle 查询系统所有用户信息
1.查看所有用户:select * from dba_users; select * from all_users; select * from user_users; 2.查看用户或角色系统 ...
- 项目名 的在JSP或JAVA中的另类写法
在JSP页面中${pageContext.request.contextPath } 表示项目名<form action="${pageContext.request.contextP ...
- 为图片添加九宫格信息-UI界面编辑器(SkinStudio)教程
1.1. 为图片添加九宫格信息 在UI编辑器中添加一张图片 使用Photoshop打开这张图片 放大这张图片,使用切片工具,选取LeftTop(左上角固定不变的位置) 双击切片,找到尺寸信息 将尺 ...
- UART UVM验证平台平台搭建总结
tb_top是整个UVM验证平台的最顶层:tb_top中例化dut,提供时钟和复位信号,定义接口以及设置driver和monitor的virual interface,在intial中调用run_te ...