dfgg

C++ code posted
created at 15 Oct 17:47, updated at 19 Oct 20:08

Edit | Back
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
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);
    }
}
2.77 KB in 10 ms with coderay