對(duì)象Equals相等性比較的通用實(shí)現(xiàn)-創(chuàng)新互聯(lián)

最近編碼的過(guò)程中,使用了對(duì)象本地內(nèi)存緩存,緩存用了Dictionary<string,object>, ConcurrentDictionary<string,oject>,還可以是MemoryCache(底層基于Hashtable)。使用緩存,肯定要處理數(shù)據(jù)變化緩存同步的問(wèn)題。如何比較數(shù)據(jù)的變化,演進(jìn)為新的內(nèi)存對(duì)象數(shù)據(jù)和已有內(nèi)存對(duì)象數(shù)據(jù)的相等性比較!

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比廉江網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式廉江網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋廉江地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

對(duì)象的Equals相等性比較,百度、google會(huì)有一大堆實(shí)現(xiàn),幾個(gè)重點(diǎn)的點(diǎn):

1. 實(shí)現(xiàn)接口IEquatable<T>

htt p s : / / ms d n . mi c r o s o f t . c o m /en-us/library/ms131190(v=vs.110).aspx

2.重寫(xiě)bool Equals(object other)方法和 int GetHashCode()方法

htt p : / / s ta  ck o v e r f l o w . co m / q u es t i o n s /2 7 3 49 1 4/whats-the-difference-between-iequatable-and-just-overriding-object-equals

這里直接貼出來(lái)一個(gè)通用實(shí)現(xiàn),分享給大家:

對(duì)象Equals相等性比較的通用實(shí)現(xiàn)

  1  /// <summary>  2     /// 流控事件  3     /// </summary>  4     [Serializable]  5     public class FlowControlEvent: IEquatable<FlowControlEvent>  6     {  7         public static readonly string Global = "Global";  8   9         /// <summary> 10         /// 標(biāo)識(shí) 11         /// </summary> 12         public string ID { get; set; } 13  14         /// <summary> 15         /// 流控策略名稱 16         /// </summary> 17         public string StrategyName { get; set; } 18  19         /// <summary> 20         /// 是否手工觸發(fā) 21         /// </summary> 22         public bool IsManuelTrigger { get; set; } 23  24         /// <summary> 25         /// 觸發(fā)時(shí)間 26         /// </summary> 27         public DateTime TriggerTime { get; set; }        
 28  29         /// <summary> 30         /// 流控策略 31         /// </summary> 32         public FlowControlStrategy Strategy { get; set; } 33  34         /// <summary> 35         /// 持續(xù)時(shí)間,單位s 36         /// </summary> 37         public long Duration { get; set; } 38  39         //是否啟用 40         private bool isEnable = true; 41  42         /// <summary> 43         /// 是否啟用 44         /// </summary> 45         public bool IsEnable 46         { 47             get 48             { 49                 return isEnable; 50             } 51             set 52             { 53                 isEnable = value; 54             } 55         } 56  57         /// <summary> 58         /// 是否使用中 59         /// </summary> 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         /// <summary> 88         /// 創(chuàng)建時(shí)間 89         /// </summary> 90         public DateTime CreateTime { get; set; } 91  92         /// <summary> 93         /// 創(chuàng)建人 94         /// </summary> 95         public string Creator { get; set; } 96  97         /// <summary> 98         /// 最后修改時(shí)間 99         /// </summary>100         public DateTime LastModifyTime { get; set; }101 102         /// <summary>103         /// 最后修改人104         /// </summary>105         public string LastModifier { get; set; }106 107         /// <summary>108         /// 相等性比較109         /// </summary>110         /// <param name="other">要比較的對(duì)象</param>111         /// <returns>true 相等 false 不相等</returns>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         /// <summary>122         /// 流控事件是否等于同一類型的另一個(gè)流控事件123         /// </summary>124         /// <param name="other">同一類型的另一個(gè)流控事件</param>125         /// <returns>true 相等 false 不相等</returns>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         /// <summary>138         /// 重載GetHashCode方法139         /// </summary>140         /// <returns>HashCode</returns>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     }

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站題目:對(duì)象Equals相等性比較的通用實(shí)現(xiàn)-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://muchs.cn/article44/pscee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站策劃網(wǎng)站內(nèi)鏈、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)