using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace LB_SmartVisionCommon
|
{
|
/// <summary>
|
/// 属性排序
|
/// </summary>
|
public class PropertySorter : ExpandableObjectConverter
|
{
|
/// <summary>
|
/// 获取支持的属性
|
/// </summary>
|
/// <param name="context">提供有关组件(例如其容器和属性描述符)的上下文信息。</param>
|
/// <returns>执行是否成功</returns>
|
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
|
{
|
return true;
|
}
|
/// <summary>
|
/// 获取属性描述符集合
|
/// </summary>
|
/// <param name="context">提供有关组件(例如其容器和属性描述符)的上下文信息。</param>
|
/// <param name="value">值</param>
|
/// <param name="attributes">自定义特性</param>
|
/// <returns>返回属性描述符集合</returns>
|
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
|
{
|
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
|
ArrayList orderedProperties = new ArrayList();
|
foreach (PropertyDescriptor pd in pdc)
|
{
|
Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
|
if (attribute != null)
|
{
|
PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
|
orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
|
}
|
else
|
{
|
orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
|
}
|
}
|
orderedProperties.Sort();
|
ArrayList propertyNames = new ArrayList();
|
foreach (PropertyOrderPair pop in orderedProperties)
|
{
|
propertyNames.Add(pop.Name);
|
}
|
return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
|
}
|
}
|
/// <summary>
|
/// 属性排序属性类
|
/// </summary>
|
[AttributeUsage(AttributeTargets.Property)]
|
public class PropertyOrderAttribute : Attribute
|
{
|
/// <summary>
|
/// 序号
|
/// </summary>
|
private int _order;
|
/// <summary>
|
/// 属性排序属性
|
/// </summary>
|
/// <param name="order">序号</param>
|
public PropertyOrderAttribute(int order)
|
{
|
_order = order;
|
}
|
/// <summary>
|
/// 属性序号
|
/// </summary>
|
public int Order
|
{
|
get
|
{
|
return _order;
|
}
|
}
|
}
|
/// <summary>
|
/// 属性顺序对类
|
/// </summary>
|
public class PropertyOrderPair : IComparable
|
{
|
private int _order;
|
private string _name;
|
/// <summary>
|
/// 名称
|
/// </summary>
|
public string Name
|
{
|
get
|
{
|
return _name;
|
}
|
}
|
/// <summary>
|
/// 属性顺序对
|
/// </summary>
|
/// <param name="name">名称</param>
|
/// <param name="order">序号</param>
|
public PropertyOrderPair(string name, int order)
|
{
|
_order = order;
|
_name = name;
|
}
|
/// <summary>
|
/// 比对排序
|
/// </summary>
|
/// <param name="obj">自定义属性</param>
|
/// <returns></returns>
|
public int CompareTo(object obj)
|
{
|
int otherOrder = ((PropertyOrderPair)obj)._order;
|
if (otherOrder == _order)
|
{
|
string otherName = ((PropertyOrderPair)obj)._name;
|
return string.Compare(_name, otherName);
|
}
|
else if (otherOrder > _order)
|
{
|
return -1;
|
}
|
return 1;
|
}
|
}
|
}
|