static PropertyCompare() { PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public); var firstObject = Expression.Parameter(typeof(T), "a"); var secondObject = Expression.Parameter(typeof(T), "b"); PropertyComparer <T>[] propertyComparers = new PropertyComparer <T> [properties.Length]; for (int i = 0; i < properties.Length; i++) { PropertyInfo thisProperty = properties[i]; Expression arePropertiesEqual = Expression.Equal(Expression.Property(firstObject, thisProperty), Expression.Property(secondObject, thisProperty)); Expression <Func <T, T, bool> > equalityFunc = Expression.Lambda <Func <T, T, bool> >(arePropertiesEqual, firstObject, secondObject); PropertyComparer <T> comparer = new PropertyComparer <T>() { Compare = equalityFunc.Compile(), PropertyName = properties[i].Name }; propertyComparers[i] = comparer; } ChangedProps = new Func <T, T, List <string> >((a, b) => { List <string> changedFields = new List <string>(); foreach (PropertyComparer <T> comparer in propertyComparers) { if (comparer.Compare(a, b)) { continue; } changedFields.Add(comparer.PropertyName); } return(changedFields); }); }
public void TestCompareThroughRelationship() { //---------------Set up test pack------------------- Car car1 = new Car(); car1.CarRegNo = "5"; Car car2 = new Car(); car2.CarRegNo = "2"; Engine engine1 = new Engine(); engine1.CarID = car1.CarID; engine1.EngineNo = "20"; Engine engine2 = new Engine(); engine2.CarID = car2.CarID; engine2.EngineNo = "50"; ITransactionCommitter committer = BORegistry.DataAccessor.CreateTransactionCommitter(); committer.AddBusinessObject(car1); committer.AddBusinessObject(car2); committer.AddBusinessObject(engine1); committer.AddBusinessObject(engine2); committer.CommitTransaction(); //---------------Assert PreConditions--------------- //---------------Execute Test ---------------------- PropertyComparer<Engine, string> comparer = new PropertyComparer<Engine, string>("CarRegNo"); comparer.Source = new Source("Car"); int comparisonResult = comparer.Compare(engine1, engine2); //---------------Test Result ----------------------- Assert.Greater(comparisonResult, 0, "engine1 should be greater as its car's regno is greater"); //---------------Tear Down ------------------------- }
public void TestSimpleCompare_String() { //---------------Set up test pack------------------- Car car1 = new Car(); car1.CarRegNo = "5"; Car car2 = new Car(); car2.CarRegNo = "2"; //---------------Execute Test ---------------------- PropertyComparer<Car, string> comparer = new PropertyComparer<Car, string>("CarRegNo"); int comparisonResult = comparer.Compare(car1, car2); //---------------Test Result ----------------------- Assert.Greater(comparisonResult, 0, "car1 should be greater than car2 when compared on CarRegNo"); //---------------Tear Down ------------------------- }
public void TestSimpleCompare_String() { //---------------Set up test pack------------------- Car car1 = new Car(); car1.CarRegNo = "5"; Car car2 = new Car(); car2.CarRegNo = "2"; //---------------Execute Test ---------------------- PropertyComparer <Car, string> comparer = new PropertyComparer <Car, string>("CarRegNo"); int comparisonResult = comparer.Compare(car1, car2); //---------------Test Result ----------------------- Assert.Greater(comparisonResult, 0, "car1 should be greater than car2 when compared on CarRegNo"); //---------------Tear Down ------------------------- }
public void TestCompareThroughRelationship() { //---------------Set up test pack------------------- Car car1 = new Car(); car1.CarRegNo = "5"; Car car2 = new Car(); car2.CarRegNo = "2"; Engine engine1 = new Engine(); engine1.CarID = car1.CarID; engine1.EngineNo = "20"; Engine engine2 = new Engine(); engine2.CarID = car2.CarID; engine2.EngineNo = "50"; ITransactionCommitter committer = BORegistry.DataAccessor.CreateTransactionCommitter(); committer.AddBusinessObject(car1); committer.AddBusinessObject(car2); committer.AddBusinessObject(engine1); committer.AddBusinessObject(engine2); committer.CommitTransaction(); //---------------Assert PreConditions--------------- //---------------Execute Test ---------------------- PropertyComparer <Engine, string> comparer = new PropertyComparer <Engine, string>("CarRegNo"); comparer.Source = new Source("Car"); int comparisonResult = comparer.Compare(engine1, engine2); //---------------Test Result ----------------------- Assert.Greater(comparisonResult, 0, "engine1 should be greater as its car's regno is greater"); //---------------Tear Down ------------------------- }
// this returns an array list of the propertydescriptor arrays, one for each // component // private static ArrayList GetCommonProperties(object[] objs, bool presort, PropertyTab tab, GridEntry parentEntry) { PropertyDescriptorCollection[] propCollections = new PropertyDescriptorCollection[objs.Length]; Attribute[] attrs = new Attribute[parentEntry.BrowsableAttributes.Count]; parentEntry.BrowsableAttributes.CopyTo(attrs, 0); for (int i = 0; i < objs.Length; i++) { PropertyDescriptorCollection pdc = tab.GetProperties(parentEntry, objs[i], attrs); if (presort) { pdc = pdc.Sort(PropertyComparer); } propCollections[i] = pdc; } ArrayList mergedList = new ArrayList(); PropertyDescriptor[] matchArray = new PropertyDescriptor[objs.Length]; // // Merge the property descriptors // int[] posArray = new int[propCollections.Length]; for (int i = 0; i < propCollections[0].Count; i++) { PropertyDescriptor pivotDesc = propCollections[0][i]; bool match = pivotDesc.Attributes[typeof(MergablePropertyAttribute)].IsDefaultAttribute(); for (int j = 1; match && j < propCollections.Length; j++) { if (posArray[j] >= propCollections[j].Count) { match = false; break; } // check to see if we're on a match // PropertyDescriptor jProp = propCollections[j][posArray[j]]; if (pivotDesc.Equals(jProp)) { posArray[j] += 1; if (!jProp.Attributes[typeof(MergablePropertyAttribute)].IsDefaultAttribute()) { match = false; break; } matchArray[j] = jProp; continue; } int jPos = posArray[j]; jProp = propCollections[j][jPos]; match = false; // if we aren't on a match, check all the items until we're past // where the matching item would be while (PropertyComparer.Compare(jProp, pivotDesc) <= 0) { // got a match! if (pivotDesc.Equals(jProp)) { if (!jProp.Attributes[typeof(MergablePropertyAttribute)].IsDefaultAttribute()) { match = false; jPos++; } else { match = true; matchArray[j] = jProp; posArray[j] = jPos + 1; } break; } // try again jPos++; if (jPos < propCollections[j].Count) { jProp = propCollections[j][jPos]; } else { break; } } // if we got here, there is no match, quit for this guy if (!match) { posArray[j] = jPos; break; } } // do we have a match? if (match) { matchArray[0] = pivotDesc; mergedList.Add(matchArray.Clone()); } } return(mergedList); }
public int CompareTo(PatternUst other) { return(propertyComparer.Compare(this, other)); }