C3032
3 天以前 ebcc1d53f14112363bbf539bcaf0202aadcdc9d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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;
        }
    }
}