Title / Description
Code public static class extAllAccess { static System.Collections.Generic.Dictionary<Type, System.Collections.Generic.Dictionary<string, System.Reflection.MemberInfo>> dtsm = new Dictionary<Type, Dictionary<string, System.Reflection.MemberInfo>>(); static void InitType(Type t, Type tDump) { if (tDump == null) { dtsm[t] = new Dictionary<string, System.Reflection.MemberInfo>(); tDump = t; } foreach (var mi in tDump.GetMembers( System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public)) dtsm[t][mi.Name] = mi; if (tDump.BaseType != null) InitType(t, tDump.BaseType); } static void CheckType(Type t) { if (t == null) return; if (!dtsm.ContainsKey(t)) { lock (dtsm) { if (!dtsm.ContainsKey(t)) InitType(t, null); } } } public static object GetField_AllAccess(this object obj, string fieldName) { if (obj == null) return null; var t = obj.GetType(); CheckType(t); return ((System.Reflection.FieldInfo)dtsm[t][fieldName]).GetValue(obj); } public static void SetField_AllAccess(this object obj, string fieldName, object value) { if (obj == null) return; var t = obj.GetType(); CheckType(t); ((System.Reflection.FieldInfo)dtsm[t][fieldName]).SetValue(obj, value); } public static object Invoke_AllAccess(this object obj, string methodName, params object[] paras) { if (obj == null) return null; var t = obj.GetType(); CheckType(t); return ((System.Reflection.MethodInfo)dtsm[t][methodName]).Invoke(obj, paras); } public static object GetProperty_AllAccess(this object obj, string propertyName, params object[] index) { if (obj == null) return null; var t = obj.GetType(); CheckType(t); return ((System.Reflection.PropertyInfo) dtsm[t][propertyName]).GetValue(obj, index.Length == 0 ? null : index); } public static void SetProperty_AllAccess(this object obj, string propertyName, object value, params object[] index) { if (obj == null) return; var t = obj.GetType(); CheckType(t); ((System.Reflection.PropertyInfo)dtsm[t][propertyName]).SetValue(obj, value, index.Length == 0 ? null : index); } }
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code