数据模型类对比 反射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的界面长这样:
随机推荐
- ssh配置文件说明
配置“/etc/ssh/ssh_config”文件 “/etc/ssh/ssh_config” 文件是OpenSSH系统范围的配置文件,允许你通过设置不同的选项来改变客户端程序的运行方式.这个文件的每 ...
- POJ C Looooops
Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; ...
- 黑马程序员_ C语言基础(一)
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ 开发过程: 编写->编译(只编译源文件,编译成*.o 只会检测语法是否合理,不会检测函数是 ...
- mvc之页面强类型
为什么使用页面强类型: 一个页面只能定义 为一个强类型.因为 我们自己写@Html.TextBox("Qq"); 有可能写错,所以我们就在 编译阶段就把页当作一个类型然后使用lam ...
- git 放弃本地修改 强制更新
git reset --hard origin/master
- Python自动化 【第六篇】:Python基础-面向对象
目录: 面向过程VS面向对象 面向对象编程介绍 为什么要用面向对象进行开发 面向对象的特性:封装.继承.多态 面向过程 VS 面向对象 面向过程编程(Procedural Programming) ...
- 使用linux的nc来进行文件的传输
NAME nc - arbitrary TCP and UDP connections and listensSYNOPSIS nc [-46DdhklnrStUuvz] [-i interv ...
- ComboBox的数据绑定
这里的ComboBox指System.Windows.Forms中的ComboBox. 使用对象数据源 IList<Model> models = ModelService.GetAllM ...
- socket笔记
参考: http://www.cnblogs.com/dolphinX/p/3460545.html http://www.cnblogs.com/wei2yi/archive/2011/03/23/ ...
- Delphi关于记录文件的操作
http://www.cnblogs.com/railgunman/archive/2010/08/16/1801004.html Delphi关于记录文件的操作 本例子几个变量的说明TFileR ...