Sonarr/Marr.Data/QGen/SortColumn.cs

76 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-05-07 02:32:43 +00:00
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
namespace Marr.Data.QGen
{
public class SortColumn<T>
{
2013-05-07 02:32:43 +00:00
[Obsolete("Use ListSortDirection instead")]
public SortColumn(Expression<Func<T, object>> sortExpression, SortDirection direction)
{
MemberExpression me = GetMemberExpression(sortExpression.Body);
DeclaringType = me.Expression.Type;
PropertyName = me.Member.Name;
2013-05-07 02:32:43 +00:00
Direction = GetSortDirection(direction);
}
2013-05-07 02:32:43 +00:00
[Obsolete("Use ListSortDirection instead")]
public SortColumn(Type declaringType, string propertyName, SortDirection direction)
2013-05-07 02:32:43 +00:00
{
DeclaringType = declaringType;
PropertyName = propertyName;
Direction = GetSortDirection(direction);
}
public SortColumn(Expression<Func<T, object>> sortExpression, ListSortDirection direction)
{
MemberExpression me = GetMemberExpression(sortExpression.Body);
DeclaringType = me.Expression.Type;
PropertyName = me.Member.Name;
Direction = direction;
}
public SortColumn(Type declaringType, string propertyName, ListSortDirection direction)
{
DeclaringType = declaringType;
PropertyName = propertyName;
Direction = direction;
}
2013-05-07 02:32:43 +00:00
public ListSortDirection Direction { get; private set; }
public Type DeclaringType { get; private set; }
public string PropertyName { get; private set; }
private MemberExpression GetMemberExpression(Expression exp)
{
MemberExpression me = exp as MemberExpression;
if (me == null)
{
var ue = exp as UnaryExpression;
me = ue.Operand as MemberExpression;
}
return me;
}
2013-05-07 02:32:43 +00:00
private ListSortDirection GetSortDirection(SortDirection direction)
{
if (direction == SortDirection.Desc) return ListSortDirection.Descending;
return ListSortDirection.Ascending;
}
}
public enum SortDirection
{
Asc,
Desc
}
}