/// <summary> /// 重写排序 /// </summary> /// <param name="prop"></param> /// <param name="direction"></param> protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction) { lock (this.SyncRoot) { if (prop.PropertyType.GetInterface("IComparable") == null) { //如果成员没有实现IComparable接口,则直接返回,不支持排序 throw new Exception(string.Format("成员:{0}没有实现IComparable接口,该字段列不支持排序", prop.Name)); } int count = this.Count; ComparerResultType result = ComparerResultType.Equal; T tmpItem = default(T); object leftValue = null; object rightValue = null; int compareResult = 0; for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { leftValue = prop.GetValue(this.Items[i]); rightValue = prop.GetValue(this.Items[j]); if (leftValue == null && rightValue == null) { result = ComparerResultType.Equal; } else if (leftValue == null && rightValue != null) { result = ComparerResultType.Less; } else if (leftValue != null && rightValue == null) { result = ComparerResultType.Greater; } else { compareResult = ((IComparable)(leftValue)).CompareTo((IComparable)(rightValue)); if (compareResult == 1) { result = ComparerResultType.Greater; } else if (compareResult == 0) { result = ComparerResultType.Equal; } else if (compareResult == -1) { result = ComparerResultType.Less; } else { throw new Exception(string.Format("奇怪的比较结果:{0}", compareResult)); } } if (result == ComparerResultType.Equal) { continue; } if (direction == ListSortDirection.Ascending) { if (result == ComparerResultType.Greater) { tmpItem = this.Items[i]; this.Items[i] = this.Items[j]; this.Items[j] = tmpItem; } } else { if (result == ComparerResultType.Less) { tmpItem = this.Items[i]; this.Items[i] = this.Items[j]; this.Items[j] = tmpItem; } } } } } this.OnListChanged(new ListChangedEventArgs(ListChangedType.ItemMoved, prop)); }
public List <EqualsResult> CompareSimple(int index1, int index2, ComparerResultType type) { VerifySessionsLoaded(); if (_sessions1.Length <= index1) { throw new Exception("Out of index in session1: " + index1); } if (_sessions2.Length <= index2) { throw new Exception("Out of index in session2: " + index2); } var res = new List <EqualsResult>(); var session1 = _sessions1[index1]; var session2 = _sessions2[index2]; // It searches the URL and it's parameters looking for differences var method1 = session1.oRequest.headers.HTTPMethod; var method2 = session2.oRequest.headers.HTTPMethod; res.Add(new EqualsResult("HTTP_Method", method1, method2)); var params1 = GetParametersFromURL(session1.fullUrl); var params2 = GetParametersFromURL(session2.fullUrl); res.Add(new EqualsResult("URL_Parameters", params1, params2)); // One of the URLs must have parameters in the URL if (params1 != null || params2 != null) { params1 = Decode(params1); params2 = Decode(params2); // It goes through all the parameters in the URL that has more of them, but it has to mantain the order to show the results var recorroURL1 = params1.Split(',').Length >= params2.Split(',').Length; var primario = recorroURL1 ? params1.Split(',') : params2.Split(','); var secundario = recorroURL1 ? params2.Split(',') : params1.Split(','); for (var i = 0; i < primario.Length; i++) { var aux = "URL_Param_" + (i + 1); var sec = (i < secundario.Length) ? secundario[i] : "NULL"; res.Add((recorroURL1) ? new EqualsResult(aux, primario[i], sec) : new EqualsResult(aux, sec, primario[i])); } } if (GetPathFromURL(session1.fullUrl) != GetPathFromURL(session2.fullUrl)) { res.Add(new EqualsResult("URL_Path", GetPathFromURL(session1.fullUrl), GetPathFromURL(session2.fullUrl))); } else if (method1 == "POST" && method2 == "POST") { // If both requests are POST, you have to compare the parameters in the bodies params1 = session1.GetRequestBodyAsString(); params2 = session2.GetRequestBodyAsString(); var dicParams1 = CreateListOfValuesFromString(index1, params1); var dicParams2 = CreateListOfValuesFromString(index2, params2); res.AddRange(GetTheDifferences(dicParams1, dicParams2, type)); } return(res); }