Files
UnrealEngineUWP/Engine/Source/Programs/Windows/CompileTimeAnalyzer/CompileTimeAnalyzer/TreeGridElementComparer.cs
Ben Marsh 0cc6e3dca6 Copying //UE4/Dev-Build to Dev-Main (//UE4/Dev-Main)
#rb none
#rnx

[CL 6631504 by Ben Marsh in Main branch]
2019-05-24 11:51:54 -04:00

45 lines
1.5 KiB
C#

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Timing_Data_Investigator
{
public class PropertyComparer<TClassType> : IComparer<TClassType>
{
private string sCompareProperty;
private bool bSortDescending;
public PropertyComparer(string CompareProperty, bool SortDescending)
{
sCompareProperty = CompareProperty;
bSortDescending = SortDescending;
}
public int Compare(TClassType x, TClassType y)
{
PropertyInfo ComparePropertyInfo = typeof(TClassType).GetProperties().FirstOrDefault(p => p.Name == sCompareProperty);
if (ComparePropertyInfo == null)
{
throw new InvalidOperationException($"Class '{nameof(TClassType)}' does not contain a property named '{sCompareProperty}'!");
}
if (!ComparePropertyInfo.PropertyType.GetInterfaces().Any(i => i.Name.Contains("IComparable")))
{
throw new InvalidOperationException($"Property type '{ComparePropertyInfo.PropertyType.Name}' is not an IComparable!");
}
IComparable xPropValue = ComparePropertyInfo.GetValue(x) as IComparable;
IComparable yPropValue = ComparePropertyInfo.GetValue(y) as IComparable;
if (bSortDescending)
{
return yPropValue.CompareTo(xPropValue);
}
return xPropValue.CompareTo(yPropValue);
}
}
}