qin
2024-09-28 e358beb08f5be49703009b64f058ecfbcfeefbd9
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
using HStation.RevitDev.Model.ModelEnum;
using System;
using System.ComponentModel;
using System.Reflection;
 
 
namespace HStation.RevitDev.RevitDataExport.Common
{
    /// <summary>
    /// 描述特性的读取扩展类
    /// </summary>
    public static class DescriptionExtension
    {
        /// <summary>
        /// 获取枚举的描述信息
        /// </summary>
        public static string GetDescription(this RevitType em)
        {
            Type type = em.GetType();
            FieldInfo fd = type.GetField(em.ToString());
            string des = fd.GetDescription();
            return des;
        }
 
 
 
        /// <summary>
        /// 获取属性的描述信息
        /// </summary>
        public static string GetDescription(this Type type, string proName)
        {
            PropertyInfo pro = type.GetProperty(proName);
            string des = proName;
            if (pro != null)
            {
                des = pro.GetDescription();
            }
            return des;
        }
 
 
 
        /// <summary>
        /// 获取属性的描述信息
        /// </summary>
        public static string GetDescription(this MemberInfo info)
        {
            var attrs = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
            string des = info.Name;
            foreach (DescriptionAttribute attr in attrs)
            {
                des = attr.Description;
            }
            return des;
        }
 
        public static string GetDescriptioin(this Enum info)
        {
            Type enumType = info.GetType();
            MemberInfo[] memberInfo = enumType.GetMembers();
            foreach (MemberInfo member in memberInfo)
            {
                string enumName = member.Name;
                if (enumName == info.ToString())
                {
                    var attrs = member.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (attrs.Length == 0) { return string.Empty; }
 
                    DescriptionAttribute attr = attrs[0] as DescriptionAttribute;
                    return attr.Description;
                }
            }
            return string.Empty;
        }
    }
}