博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对象Equals相等性比较的通用实现
阅读量:6986 次
发布时间:2019-06-27

本文共 5040 字,大约阅读时间需要 16 分钟。

最近编码的过程中,使用了对象本地内存缓存,缓存用了Dictionary<string,object>, ConcurrentDictionary<string,oject>,还可以是MemoryCache(底层基于Hashtable)。使用缓存,肯定要处理数据变化缓存同步的问题。如何比较数据的变化,演进为新的内存对象数据和已有内存对象数据的相等性比较!

对象的Equals相等性比较,百度、google会有一大堆实现,几个重点的点:

1. 实现接口IEquatable<T>

https://msdn.microsoft.com/en-us/library/ms131190(v=vs.110).aspx

2.重写bool Equals(object other)方法和 int GetHashCode()方法

http://stackoverflow.com/questions/2734914/whats-the-difference-between-iequatable-and-just-overriding-object-equals

这里直接贴出来一个通用实现,分享给大家:

1  ///   2     /// 流控事件  3     ///   4     [Serializable]  5     public class FlowControlEvent: IEquatable
6 { 7 public static readonly string Global = "Global"; 8 9 ///
10 /// 标识 11 /// 12 public string ID { get; set; } 13 14 ///
15 /// 流控策略名称 16 /// 17 public string StrategyName { get; set; } 18 19 ///
20 /// 是否手工触发 21 /// 22 public bool IsManuelTrigger { get; set; } 23 24 ///
25 /// 触发时间 26 /// 27 public DateTime TriggerTime { get; set; } 28 29 ///
30 /// 流控策略 31 /// 32 public FlowControlStrategy Strategy { get; set; } 33 34 ///
35 /// 持续时间,单位s 36 /// 37 public long Duration { get; set; } 38 39 //是否启用 40 private bool isEnable = true; 41 42 ///
43 /// 是否启用 44 /// 45 public bool IsEnable 46 { 47 get 48 { 49 return isEnable; 50 } 51 set 52 { 53 isEnable = value; 54 } 55 } 56 57 ///
58 /// 是否使用中 59 /// 60 public bool IsUsing 61 { 62 get 63 { 64 if (IsEnable == false) return false; 65 if (IsManuelTrigger) 66 { 67 if (Duration == long.MaxValue) 68 { 69 return true; 70 } 71 else 72 { 73 var span = DateTime.Now - TriggerTime; 74 if (span.TotalSeconds > Duration) 75 return false; 76 else 77 return true; 78 } 79 } 80 else 81 { 82 return true; 83 } 84 } 85 } 86 87 ///
88 /// 创建时间 89 /// 90 public DateTime CreateTime { get; set; } 91 92 ///
93 /// 创建人 94 /// 95 public string Creator { get; set; } 96 97 ///
98 /// 最后修改时间 99 /// 100 public DateTime LastModifyTime { get; set; }101 102 ///
103 /// 最后修改人104 /// 105 public string LastModifier { get; set; }106 107 ///
108 /// 相等性比较109 /// 110 ///
要比较的对象111 ///
true 相等 false 不相等
112 public override bool Equals(object other)113 {114 if (ReferenceEquals(null, other)) return false;115 if (ReferenceEquals(this, other)) return true;116 if (other.GetType() != this.GetType()) return false;117 118 return Equals((FlowControlEvent)other);119 }120 121 ///
122 /// 流控事件是否等于同一类型的另一个流控事件123 /// 124 ///
同一类型的另一个流控事件125 ///
true 相等 false 不相等
126 public bool Equals(FlowControlEvent other)127 {128 if (other == null)129 return false;130 if (!string.Equals(this.ID , other.ID) || this.IsEnable != other.IsEnable || this.Duration!= other.Duration131 || !string.Equals(this.StrategyName, other.StrategyName)||this.TriggerTime!= other.TriggerTime)132 return false;133 134 return true;135 }136 137 ///
138 /// 重载GetHashCode方法139 /// 140 ///
HashCode
141 public override int GetHashCode()142 {143 unchecked144 {145 var result = 0;146 result = (result * 397) ^ ID.GetHashCode();147 result = (result * 397) ^ IsEnable.GetHashCode();148 result = (result * 397) ^ Duration.GetHashCode();149 result = (result * 397) ^ StrategyName.GetHashCode();150 result = (result * 397) ^ TriggerTime.GetHashCode();151 return result;152 }153 }154 }

 

周国庆

2017/4/25

 

posted on
2017-05-12 10:36 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/yezuhui/p/6844387.html

你可能感兴趣的文章
张小龙公布微信小程序进展 可直接从桌面进入
查看>>
手机芯片三国杀:高通、联发科、展讯都想成霸主
查看>>
六大技巧提升员工信息安全意识
查看>>
保利协鑫多晶硅产量再创历史记录
查看>>
爱屋及乌 年轻投资者因喜爱Snapchat亏钱也买Snap股票
查看>>
物联网产品背后潜藏着危机
查看>>
阿里云将增设马来西亚数据中心 中国技术获赞
查看>>
与Netflix合作 美电视运营商推出4K频道
查看>>
Struts2中的Action
查看>>
Balluff推出刀具识别系统
查看>>
美国支付巨头Verifone遭遇网络攻击
查看>>
开平推进智慧城市等领域信息化建设及公共数据资源共享
查看>>
宜兴电信成功跨界合作开拓农村物联网市场
查看>>
Oracle业务适合用PostgreSQL去O的一些评判标准
查看>>
多个常见代码设计缺陷
查看>>
今年光伏市场规模可达30GW 分布式有望占据三分江山
查看>>
因新漏洞问题 Firefox 49发布时间将延期一周
查看>>
WLAN产品形态之分层架构
查看>>
Chrome 隐藏 SSL 证书信息 禁止禁用 DRM
查看>>
《Windows Server 2012 Hyper-V虚拟化管理实践》——3.2 Hyper-V主机日常管理
查看>>