C#使用反射获取对象变化的情况
记录日志时, 经常需要描述对象的状态发生了怎样的变化, 以前处理的非常简单粗暴:
a. 重写class的ToString()方法, 将重要的属性都输出来
b. 记录日志时: 谁谁谁 由 变更前实例.ToString() 变成 变更后实例.ToString()
但输出的日志总是太长了, 翻看日志时想找到差异也非常麻烦, 所以想输出为: 谁谁谁的哪个属性由 aaa 变成了 bbb
手写代码一个一个的比较字段然后输出这样的日志信息, 是不敢想象的事情. 本来想参考Dapper使用 System.Reflection.Emit 发射 来提高运行效率, 但实在没有功夫研究.Net Framework的中间语言, 所以准备用 Attribute特性 和 反射 来实现
/// <summary>
/// 要比较的字段或属性, 目前只支持C#基本类型, 比如 int, bool, string等, 你自己写的class或者struct 需要重写 ToString()、Equals(), 按理说如果重写了Equals(), 那也需要重写GetHashCode(), 但确实没有用到GetHashCode(), 所以可以忽略Warning不重写GetHashCode();
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public class ComparePropertyFieldAttribute : Attribute
{
/// <summary>
/// 属性或字段的别名
/// </summary>
public string PropertyName { get; private set; } /// <summary>
/// 要比较的字段或属性
/// </summary>
public ComparePropertyFieldAttribute()
{ } /// <summary>
/// 要比较的字段或属性
/// </summary>
/// <param name="propertyName">属性或字段的别名</param>
public ComparePropertyFieldAttribute(string propertyName)
{
PropertyName = propertyName;
} // 缓存反射的结果, Tuple<object, ComparePropertyAttribute> 中第一个参数之所以用object 是因为要保存 PropertyInfo 和 FieldInfo
private static Dictionary<Type, Tuple<object, ComparePropertyFieldAttribute>[]> dict = new Dictionary<Type, Tuple<object, ComparePropertyFieldAttribute>[]>(); /// <summary>
/// 只对带有ComparePropertyAttribute的属性和字段进行比较
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="differenceMsg">不相同的字段或属性 的字符串说明</param>
/// <returns>两者相同时, true; 两者不相同时, false</returns>
public static bool CompareDifference<T>(T from, T to, out string differenceMsg)
{
var type = typeof(T);
lock (dict)
{
if (!dict.ContainsKey(type))
{
var list = new List<Tuple<object, ComparePropertyFieldAttribute>>();
// 获取带ComparePropertyAttribute的属性
var properties = type.GetProperties();
foreach (var property in properties)
{
var comparePropertyAttribute = (ComparePropertyFieldAttribute)property.GetCustomAttributes(typeof(ComparePropertyFieldAttribute), false).FirstOrDefault();
if (comparePropertyAttribute != null)
{
list.Add(Tuple.Create<object, ComparePropertyFieldAttribute>(property, comparePropertyAttribute));
}
}
// 获取带ComparePropertyAttribute字段
var fields = type.GetFields();
foreach (var field in fields)
{
var comparePropertyAttribute = (ComparePropertyFieldAttribute)field.GetCustomAttributes(typeof(ComparePropertyFieldAttribute), false).FirstOrDefault();
if (comparePropertyAttribute != null)
{
list.Add(Tuple.Create<object, ComparePropertyFieldAttribute>(field, comparePropertyAttribute));
}
} dict.Add(type, list.ToArray());
}
} var sb = new StringBuilder(); //估计200字节能覆盖大多数情况了吧
var tupleArray = dict[type];
foreach (var tuple in tupleArray)
{
object v1 = null, v2 = null;
if (tuple.Item1 is System.Reflection.PropertyInfo)
{
if (from != null)
{
v1 = ((System.Reflection.PropertyInfo)tuple.Item1).GetValue(from, null);
}
if (to != null)
{
v2 = ((System.Reflection.PropertyInfo)tuple.Item1).GetValue(to, null);
}
if (!object.Equals(v1, v2))
{
sb.AppendFormat("{0}从 {1} 变成 {2}; ", tuple.Item2.PropertyName ?? ((System.Reflection.PropertyInfo)tuple.Item1).Name, v1 ?? "null", v2 ?? "null");
}
}
else if (tuple.Item1 is System.Reflection.FieldInfo)
{
if (from != null)
{
v1 = ((System.Reflection.FieldInfo)tuple.Item1).GetValue(from);
}
if (to != null)
{
v2 = ((System.Reflection.FieldInfo)tuple.Item1).GetValue(to);
}
if (!object.Equals(v1, v2))
{
sb.AppendFormat("{0}从 {1} 变成 {2}; ", tuple.Item2.PropertyName ?? ((System.Reflection.FieldInfo)tuple.Item1).Name, v1 ?? "null", v2 ?? "null");
}
}
} differenceMsg = sb.ToString();
return differenceMsg == "";
}
}
ComparePropertyFieldAttribute
使用方法:
1. 将重要字段或属性加上 [ComparePropertyField] 特性, 目前只支持C#基本类型, 比如 int, bool, string等, 你自己写的class或者struct 需要重写 ToString()、Equals(), 按理说如果重写了Equals(), 那也需要重写GetHashCode(), 但确实没有用到GetHashCode(), 所以可以忽略Warning不重写GetHashCode()
2. 使用ComparePropertyFieldAttribute.CompareDifference 比较变更前后的实例即可
具体可参考下面的示例
class Program
{
static void Main(string[] args)
{
// 请用Debug测试, Release会优化掉一些代码导致测试不准确
System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
var p1 = new Person() { INT = , BOOL = false, S = "p1", S2 = "p1" };
var p2 = new Person() { INT = , BOOL = false, S = "p1", S2 = "p1" };
string msg = null; stopwatch.Start();
for (int i = ; i < ; i++)
{
if (!p1.Equals(p2))
{
msg = string.Format("{0} 变成 {1}", p1.ToString(), p2.ToString());
}
}
stopwatch.Stop();
Console.WriteLine("原生比较结果: " + msg);
Console.WriteLine("原生比较耗时: " + stopwatch.Elapsed); stopwatch.Start();
for (int i = ; i < ; i++)
{
var result = ComparePropertyFieldAttribute.CompareDifference<Person>(p1, p2, out msg);
}
stopwatch.Stop();
Console.WriteLine("ComparePropertyAttribute比较结果: " + msg);
Console.WriteLine("ComparePropertyAttribute比较: " + stopwatch.Elapsed); Console.ReadLine();
}
} public class Person
{
[ComparePropertyField]
public int INT { get; set; } [ComparePropertyFieldAttribute("布尔")]
public bool BOOL { get; set; } [ComparePropertyFieldAttribute("字符串")]
public string S { get; set; } [ComparePropertyFieldAttribute("S22222")]
public string S2; public override bool Equals(object obj)
{
var another = obj as Person;
if (another==null)
{
return false;
}
return this.INT == another.INT &&
this.BOOL == another.BOOL &&
this.S == another.S &&
this.S2 == another.S2;
} public override string ToString()
{
return string.Format("i={0}, 布尔={1}, 字符串={2}, S22222={3}", INT, BOOL, S, S2);
}
}

耗时是原生的3倍, 考虑到只有记录日志才使用这个, 使用的机会很少, 对性能的损耗可以认为非常小.
end
C#使用反射获取对象变化的情况的更多相关文章
- Java反射获取对象成员属性,getFields()与getDeclaredFields()方法的区别
Java反射获取对象成员属性,getFields()与getDeclaredFields()方法的区别 在工作中遇到一个问题,就是你需要去判断某个字符串是不是对象的某个成员属性名,然后根据判断结果 ...
- Java反射获取对象VO的属性值(通过Getter方法)
有时候,需要动态获取对象的属性值. 比如,给你一个List,要你遍历这个List的对象的属性,而这个List里的对象并不固定.比如,这次User,下次可能是Company. e.g. 这次我需要做一个 ...
- java利用反射获取对象前后修改的内容(用于日志记录)
import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...
- 第五课 JAVA反射获取对象属性和方法(通过配置文件)
Service1.java package reflection; public class Service1 { public void doService1(){ System.out.print ...
- C#通过反射获取对象属性,打印所有字段属性的值
获取所有字段的值: public void PrintProperties(Object obj) { Type type = obj.GetType(); foreach( PropertyInfo ...
- 第五课 JAVA反射获取对象属性和方法
package com.hero; import java.lang.reflect.Field; public class TestReflction5 { public static void m ...
- 利用反射获取对象中的值等于x的字段
Field[] field = behavior.getClass().getDeclaredFields(); for (int i = 0; i < field.length; i++) { ...
- c#利用反射获取对象属性值
public static string GetObjectPropertyValue<T>(T t, string propertyname){ Type type = type ...
- JAVA使用反射获取对象的所有属性名
public static void main(String[] args) { Field[] fields=BaseSalary.class.getDeclaredFields(); for (i ...
随机推荐
- Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)
Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...
- js长整型的失真问题解决
遇到的问题: 后端返回的订单号是整型的,超过了Math.pow(2,53) = 9007199254740992,导致获取的数据失真. 类似问题:https://www.zhihu.com/quest ...
- C 语言 计算
note1: 不要把表达式写的太复杂,不容易阅读和理解,容易造成读程序的误解,所以要避免写出复杂的表达式. note2:程序是按步执行的,程序表达的是顺序执行的动作,而不是关系.
- scrapy爬去京东书籍信息
# -*- coding: utf-8 -*- import scrapy import urllib import json from copy import deepcopy class JdSp ...
- mysql 动态行转列
表结果:create table user( id int , username ), create_time datetime, type int ) insert into user (`id`, ...
- java 连接SQL Server
1.确认服务器的连通性,并且使用账户密码模式登陆有效. 1).登陆服务器 2).查看安全性 2.新建数据库用于测试 3.下载jdbc安装并配置 进入微软官网主页--> 搜索JDBC-->找 ...
- 68.纯 CSS 创作一本色卡
原文地址:https://segmentfault.com/a/1190000015456554 感想:复习下 transform-origin: 位置; HTML code: <div cla ...
- 抢人就完事了——OO第二单元总结
总结性博客作业 (1)从多线程的协同和同步控制方面,分析和总结自己三次作业的设计策略. (2)基于度量来分析自己的程序结构度量类的属性个数.方法个数.每个方法规模.每个方法的控制分支数目.类总代码规模 ...
- Android Studio添加aar依赖的两种方式
1 android{ repositories { flatDir { dirs 'libs' } } } dependencies { implementation fileTree(include ...
- mac 环境搭建
安装homebrew jdk 也可以官网下载 maven 官网下载tar.gz包 也在homebrew下安装git,java,mysql.