ProjectArcade
171 строка · 4.6 Кб
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5
6namespace EmulatorLauncher.Common7{
8static class ReflectionHelper9{10private static Dictionary<Type, ReflectionProperties> _types = new Dictionary<Type, ReflectionProperties>();11private static Dictionary<Type, ReflectionFields> _fields = new Dictionary<Type, ReflectionFields>();12
13public static ReflectionProperties GetReflectionProperties(this Type type)14{15ReflectionProperties prop;16if (_types.TryGetValue(type, out prop))17return prop;18
19prop = new ReflectionProperties(type);20_types[type] = prop;21return prop;22}23
24public static ReflectionFields GetReflectionFields(this Type type)25{26ReflectionFields prop;27if (_fields.TryGetValue(type, out prop))28return prop;29
30prop = new ReflectionFields(type);31_fields[type] = prop;32return prop;33}34
35public static object GetValue(this Type t, object instance, string propertyName)36{37var rp = GetReflectionProperties(t);38var pi = rp.GetProperty(propertyName);39if (pi == null)40return null;41
42return pi.GetValue(instance);43}44
45public static T GetValue<T>(this Type t, object instance, string propertyName)46{47var rp = GetReflectionProperties(t);48var pi = rp.GetProperty(propertyName);49if (pi == null)50return default(T);51
52var obj = pi.GetValue(instance);53if (obj is T)54return (T)obj;55
56if (obj != null)57{58try { return (T)Convert.ChangeType(obj, typeof(T)); }59catch { }60}61
62return default(T);63}64
65public static T GetFieldValue<T>(this Type t, object instance, string fieldName)66{67var rp = GetReflectionFields(t);68var pi = rp.GetField(fieldName);69if (pi == null)70return default(T);71
72var obj = pi.GetValue(instance);73if (obj is T)74return (T)obj;75
76if (obj != null)77{78try { return (T)Convert.ChangeType(obj, typeof(T)); }79catch { }80}81
82return default(T);83}84}85
86class ReflectionProperty87{88private System.Reflection.PropertyInfo _property;89
90public ReflectionProperty(System.Reflection.PropertyInfo pi)91{92_property = pi;93}94
95public object GetValue(object instance, object[] index = null)96{97return _property.GetValue(instance, index);98}99}100
101class ReflectionProperties102{103private Type _type;104
105public ReflectionProperties(Type t)106{107_type = t;108}109
110public ReflectionProperty GetProperty(string name)111{112ReflectionProperty prop;113if (_properties.TryGetValue(name, out prop))114return prop;115
116var propInfo = _type.GetProperty(name);117if (propInfo == null)118return null;119
120prop = new ReflectionProperty(propInfo);121_properties[name] = prop;122return prop;123
124}125
126private Dictionary<string, ReflectionProperty> _properties = new Dictionary<string, ReflectionProperty>();127}128
129class ReflectionField130{131private System.Reflection.FieldInfo _field;132
133public ReflectionField(System.Reflection.FieldInfo pi)134{135_field = pi;136}137
138public object GetValue(object instance)139{140return _field.GetValue(instance);141}142}143
144class ReflectionFields145{146private Type _type;147
148public ReflectionFields(Type t)149{150_type = t;151}152
153public ReflectionField GetField(string name)154{155ReflectionField prop;156if (_properties.TryGetValue(name, out prop))157return prop;158
159var propInfo = _type.GetField(name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);160if (propInfo == null)161return null;162
163prop = new ReflectionField(propInfo);164_properties[name] = prop;165return prop;166
167}168
169private Dictionary<string, ReflectionField> _properties = new Dictionary<string, ReflectionField>();170}171}
172