ReflectionUtils.cs
3.88 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace UnityEditor.PostProcessing
{
public static class ReflectionUtils
{
static Dictionary<KeyValuePair<object, string>, FieldInfo> s_FieldInfoFromPaths = new Dictionary<KeyValuePair<object, string>, FieldInfo>();
public static FieldInfo GetFieldInfoFromPath(object source, string path)
{
FieldInfo field = null;
var kvp = new KeyValuePair<object, string>(source, path);
if (!s_FieldInfoFromPaths.TryGetValue(kvp, out field))
{
var splittedPath = path.Split('.');
var type = source.GetType();
foreach (var t in splittedPath)
{
field = type.GetField(t, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (field == null)
break;
type = field.FieldType;
}
s_FieldInfoFromPaths.Add(kvp, field);
}
return field;
}
public static string GetFieldPath<T, TValue>(Expression<Func<T, TValue>> expr)
{
MemberExpression me;
switch (expr.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expr.Body as UnaryExpression;
me = (ue != null ? ue.Operand : null) as MemberExpression;
break;
default:
me = expr.Body as MemberExpression;
break;
}
var members = new List<string>();
while (me != null)
{
members.Add(me.Member.Name);
me = me.Expression as MemberExpression;
}
var sb = new StringBuilder();
for (int i = members.Count - 1; i >= 0; i--)
{
sb.Append(members[i]);
if (i > 0) sb.Append('.');
}
return sb.ToString();
}
public static object GetFieldValue(object source, string name)
{
var type = source.GetType();
while (type != null)
{
var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (f != null)
return f.GetValue(source);
type = type.BaseType;
}
return null;
}
public static object GetFieldValueFromPath(object source, ref Type baseType, string path)
{
var splittedPath = path.Split('.');
object srcObject = source;
foreach (var t in splittedPath)
{
var fieldInfo = baseType.GetField(t, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (fieldInfo == null)
{
baseType = null;
break;
}
baseType = fieldInfo.FieldType;
srcObject = GetFieldValue(srcObject, t);
}
return baseType == null
? null
: srcObject;
}
public static object GetParentObject(string path, object obj)
{
var fields = path.Split('.');
if (fields.Length == 1)
return obj;
var info = obj.GetType().GetField(fields[0], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
obj = info.GetValue(obj);
return GetParentObject(string.Join(".", fields, 1, fields.Length - 1), obj);
}
}
}