数据模型类对比 反射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的界面长这样:
随机推荐
- xml 解析
例:解析以下片段 <font> <name>Helvetica</name> <size units="pt">36</siz ...
- oracle查询小结
一.查询某表中某列值相同的记录: select * from t_acct_map where acct_id in ( select acct_id from t_acct_map grou ...
- 解决:新版火狐浏览器3d打不开
重启:按 Ctrl + Shift + L 键唤出 3d 视图 参考文档:http://tieba.baidu.com/p/4606488108
- HDFS--(HA)初始化与启动
1.启动zk 2.启动journalnode: hadoop-daemons.sh start journalnode 3.格式化zkfc--让在zookeeper中生成ha节点 ...
- strong reference cycle in block
However, because the reference is weak, the object that self points to could be deallocated while th ...
- 学习了一下javascript的模块化编程
现在在我脑海里关于“模块化”的概念是这些词:简单.具有逻辑之美.易用.健壮.可扩展.似乎这些形容与我现在水平写出的代码有点格格不入啊. 所以今天想了解和简单的实践一下“模块化开发”. 1.首先学习一下 ...
- Remark
// create by kim 20140805 public void Remark_insertChangeHeader(Editor e) { userinfo userInfo; ; e.u ...
- 【Linux】netdata监控组件
github:https://github.com/firehol/netdata 安装:https://github.com/firehol/netdata/wiki/Installation 内存 ...
- python模块概况,json/pickle,time/datetime,logging
参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html http://www.cnblogs.com/alex3714/articles/51 ...
- Linux VM acquisition
The evidence is a VM as below. The flat vmdk is the real disk, and the vmdk only 1kb is just a descr ...