ProjectArcade

Форк
0
171 строка · 4.6 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5

6
namespace EmulatorLauncher.Common
7
{
8
    static class ReflectionHelper
9
    {
10
        private static Dictionary<Type, ReflectionProperties> _types = new Dictionary<Type, ReflectionProperties>();
11
        private static Dictionary<Type, ReflectionFields> _fields = new Dictionary<Type, ReflectionFields>();
12

13
        public static ReflectionProperties GetReflectionProperties(this Type type)
14
        {
15
            ReflectionProperties prop;
16
            if (_types.TryGetValue(type, out prop))
17
                return prop;
18

19
            prop = new ReflectionProperties(type);
20
            _types[type] = prop;
21
            return prop;
22
        }
23

24
        public static ReflectionFields GetReflectionFields(this Type type)
25
        {
26
            ReflectionFields prop;
27
            if (_fields.TryGetValue(type, out prop))
28
                return prop;
29

30
            prop = new ReflectionFields(type);
31
            _fields[type] = prop;
32
            return prop;
33
        }
34

35
        public static object GetValue(this Type t, object instance, string propertyName)
36
        {
37
            var rp = GetReflectionProperties(t);
38
            var pi = rp.GetProperty(propertyName);
39
            if (pi == null)
40
                return null;
41

42
            return pi.GetValue(instance);
43
        }
44

45
        public static T GetValue<T>(this Type t, object instance, string propertyName)
46
        {
47
            var rp = GetReflectionProperties(t);
48
            var pi = rp.GetProperty(propertyName);
49
            if (pi == null)
50
                return default(T);
51

52
            var obj = pi.GetValue(instance);
53
            if (obj is T)
54
                return (T)obj;
55

56
            if (obj != null)
57
            {
58
                try { return (T)Convert.ChangeType(obj, typeof(T)); }
59
                catch { }
60
            }
61

62
            return default(T);
63
        }
64

65
        public static T GetFieldValue<T>(this Type t, object instance, string fieldName)
66
        {
67
            var rp = GetReflectionFields(t);
68
            var pi = rp.GetField(fieldName);
69
            if (pi == null)
70
                return default(T);
71

72
            var obj = pi.GetValue(instance);
73
            if (obj is T)
74
                return (T)obj;
75

76
            if (obj != null)
77
            {
78
                try { return (T)Convert.ChangeType(obj, typeof(T)); }
79
                catch { }
80
            }
81

82
            return default(T);
83
        }
84
    }
85

86
    class ReflectionProperty
87
    {
88
        private System.Reflection.PropertyInfo _property;
89

90
        public ReflectionProperty(System.Reflection.PropertyInfo pi)
91
        {
92
            _property = pi;
93
        }
94

95
        public object GetValue(object instance, object[] index = null)
96
        {
97
            return _property.GetValue(instance, index);
98
        }
99
    }
100

101
    class ReflectionProperties
102
    {
103
        private Type _type;
104

105
        public ReflectionProperties(Type t)
106
        {
107
            _type = t;
108
        }
109

110
        public ReflectionProperty GetProperty(string name)
111
        {
112
            ReflectionProperty prop;
113
            if (_properties.TryGetValue(name, out prop))
114
                return prop;
115

116
            var propInfo = _type.GetProperty(name);
117
            if (propInfo == null)
118
                return null;
119

120
            prop = new ReflectionProperty(propInfo);
121
            _properties[name] = prop;
122
            return prop;
123

124
        }
125

126
        private Dictionary<string, ReflectionProperty> _properties = new Dictionary<string, ReflectionProperty>();
127
    }
128

129
    class ReflectionField
130
    {
131
        private System.Reflection.FieldInfo _field;
132

133
        public ReflectionField(System.Reflection.FieldInfo pi)
134
        {
135
            _field = pi;
136
        }
137

138
        public object GetValue(object instance)
139
        {
140
            return _field.GetValue(instance);
141
        }
142
    }
143

144
    class ReflectionFields
145
    {
146
        private Type _type;
147

148
        public ReflectionFields(Type t)
149
        {
150
            _type = t;
151
        }
152

153
        public ReflectionField GetField(string name)
154
        {
155
            ReflectionField prop;
156
            if (_properties.TryGetValue(name, out prop))
157
                return prop;
158

159
            var propInfo = _type.GetField(name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
160
            if (propInfo == null)
161
                return null;
162

163
            prop = new ReflectionField(propInfo);
164
            _properties[name] = prop;
165
            return prop;
166

167
        }
168

169
        private Dictionary<string, ReflectionField> _properties = new Dictionary<string, ReflectionField>();
170
    }
171
}
172

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.