对象Equals相等性比较的通用实现
最近编码的过程中,使用了对象本地内存缓存,缓存用了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 /// <summary> 2 /// 流控事件 3 </summary> 4 [Serializable] 5 public class FlowControlEvent: IEquatable<FlowControlEvent> 6 { 7 static readonly string Global = "Global"; 8 9 10 标识 11 12 string ID { get; set; } 13 14 15 流控策略名称 16 17 string StrategyName { 18 19 20 是否手工触发 21 22 bool IsManuelTrigger { 23 24 25 触发时间 26 27 public DateTime TriggerTime { ; } 28 29 30 流控策略 31 32 public FlowControlStrategy Strategy { 33 34 35 持续时间,单位s 36 37 long Duration { 38 39 //是否启用 40 private bool isEnable = true 41 42 43 是否启用 44 45 bool IsEnable 46 { 47 get 48 { 49 return isEnable; 50 } 51 set 52 53 isEnable = value; 54 55 } 56 57 58 是否使用中 59 60 IsUsing 61 62 63 64 if (IsEnable == false) return false 65 if (IsManuelTrigger) 66 { 67 if (Duration == long.MaxValue) 68 { 69 70 } 71 else 72 73 var span = DateTime.Now - TriggerTime; 74 if (span.TotalSeconds > Duration) 75 76 77 78 79 } 80 81 82 83 84 85 86 87 88 创建时间 89 90 public DateTime CreateTime { 91 92 93 创建人 94 95 string Creator { 96 97 98 最后修改时间 99 100 public DateTime LastModifyTime { 101 102 103 最后修改人 104 105 string LastModifier { 106 107 108 相等性比较 109 110 <param name="other">要比较的对象</param> 111 <returns>true 相等 false 不相等</returns> 112 override bool Equals(object other) 113 114 if (ReferenceEquals(null,other)) 115 this,1)">116 if (other.GetType() != this.GetType()) 117 118 Equals((FlowControlEvent)other); 119 120 121 122 流控事件是否等于同一类型的另一个流控事件 123 124 同一类型的另一个流控事件125 126 Equals(FlowControlEvent other) 127 128 if (other == null) 129 130 if (!string.Equals(this.ID,other.ID) || this.IsEnable != other.IsEnable || this.Duration!= other.Duration 131 || !this.StrategyName,other.StrategyName)||this.TriggerTime!= other.TriggerTime) 132 133 134 135 136 137 138 重载GetHashCode方法 139 140 HashCode141 int GetHashCode() 142 143 unchecked 144 145 var result = 0146 result = (result * 397) ^ ID.GetHashCode(); 147 result = (result * IsEnable.GetHashCode(); 148 result = (result * Duration.GetHashCode(); 149 result = (result * StrategyName.GetHashCode(); 150 result = (result * TriggerTime.GetHashCode(); 151 result; 152 153 154 } ? 周国庆 2017/4/25 ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 会话在20分钟后到期:是否空闲
- asp.net – 通过Ajax Post – MVC3更新模型更改视图
- asp.net-mvc – Asp.Net MVC – “动态”路由的最佳方法
- asp.net-mvc-4 – 我应该如何使用ReturnUrl = ViewBag.Retu
- asp.net-mvc – AspNet上的Kestrel vNext不提供索引页/
- asp.net读取磁盘文件、删除实例代码
- ASP.NET -- WebForm -- ScriptManager 类
- 如何从ASP.NET中的Web请求返回pdf?
- ASP.NET WebApi使用Swagger做接口文档
- asp.net HC架构
- asp.net – 生成本地资源后缺少服务器标签
- 将WebApiTestClient添加到ASP.NET Web API帮助页
- asp.net-mvc-2 – 如何在局部视图中获取与父操作
- asp.net-mvc – 如何将数据属性放在Ajax.BeginFo
- 有没有办法确保(仅)在HTTPS协议上运行ASP.NET应用
- asp.net-core – AspNetCore中的控制器配置
- asp.net – MaintainScrollPositionOnPostback属
- 我们可以在ASP.NET Webforms(* .aspx页面)中使用
- asp.net – SignalR,Owin和异常处理
- 文本框使用textmode密码不显示文本asp.net c#