com.wallstop-studios.unity-helpers 2.0.0-rc40 → 2.0.0-rc41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -72,6 +72,76 @@
|
|
|
72
72
|
#endif
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
public static Func<TInstance, TValue> GetFieldGetter<TInstance, TValue>(FieldInfo field)
|
|
76
|
+
{
|
|
77
|
+
#if WEB_GL
|
|
78
|
+
return Getter;
|
|
79
|
+
TValue Getter(TInstance instance)
|
|
80
|
+
{
|
|
81
|
+
return (TValue)field.GetValue(instance);
|
|
82
|
+
}
|
|
83
|
+
#else
|
|
84
|
+
DynamicMethod dynamicMethod = new(
|
|
85
|
+
$"GetGeneric{field.DeclaringType.Name}{field.Name}",
|
|
86
|
+
typeof(TValue),
|
|
87
|
+
new[] { typeof(TInstance) },
|
|
88
|
+
field.DeclaringType,
|
|
89
|
+
true
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
ILGenerator il = dynamicMethod.GetILGenerator();
|
|
93
|
+
|
|
94
|
+
if (!field.IsStatic)
|
|
95
|
+
{
|
|
96
|
+
if (typeof(TInstance).IsValueType)
|
|
97
|
+
{
|
|
98
|
+
il.Emit(OpCodes.Ldarga_S, 0);
|
|
99
|
+
}
|
|
100
|
+
else
|
|
101
|
+
{
|
|
102
|
+
il.Emit(OpCodes.Ldarg_0);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (field.DeclaringType != typeof(TInstance))
|
|
106
|
+
{
|
|
107
|
+
il.Emit(
|
|
108
|
+
field.DeclaringType.IsValueType ? OpCodes.Unbox : OpCodes.Castclass,
|
|
109
|
+
field.DeclaringType
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
il.Emit(OpCodes.Ldfld, field);
|
|
114
|
+
}
|
|
115
|
+
else
|
|
116
|
+
{
|
|
117
|
+
il.Emit(OpCodes.Ldsfld, field);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (field.FieldType.IsValueType)
|
|
121
|
+
{
|
|
122
|
+
if (!typeof(TValue).IsValueType)
|
|
123
|
+
{
|
|
124
|
+
il.Emit(OpCodes.Box, field.FieldType);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else
|
|
128
|
+
{
|
|
129
|
+
if (typeof(TValue).IsValueType)
|
|
130
|
+
{
|
|
131
|
+
il.Emit(OpCodes.Unbox_Any, typeof(TValue));
|
|
132
|
+
}
|
|
133
|
+
else if (typeof(TValue) != field.FieldType)
|
|
134
|
+
{
|
|
135
|
+
il.Emit(OpCodes.Castclass, typeof(TValue));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
il.Emit(OpCodes.Ret);
|
|
140
|
+
return (Func<TInstance, TValue>)
|
|
141
|
+
dynamicMethod.CreateDelegate(typeof(Func<TInstance, TValue>));
|
|
142
|
+
#endif
|
|
143
|
+
}
|
|
144
|
+
|
|
75
145
|
public static FieldSetter<TInstance, TValue> GetFieldSetter<TInstance, TValue>(
|
|
76
146
|
FieldInfo field
|
|
77
147
|
)
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
|
|
23
23
|
private readonly Random _random = new();
|
|
24
24
|
|
|
25
|
+
// TODO: Test on static fields
|
|
25
26
|
[Test]
|
|
26
27
|
public void GetFieldGetterClass()
|
|
27
28
|
{
|
|
@@ -37,6 +38,7 @@
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
// TODO: Test on static fields
|
|
40
42
|
[Test]
|
|
41
43
|
public void GetFieldGetterStruct()
|
|
42
44
|
{
|
|
@@ -52,6 +54,7 @@
|
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
// TODO: Test on static fields
|
|
55
58
|
[Test]
|
|
56
59
|
public void GetFieldSetterClass()
|
|
57
60
|
{
|
|
@@ -67,6 +70,7 @@
|
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
|
|
73
|
+
// TODO: Test on static fields
|
|
70
74
|
[Test]
|
|
71
75
|
public void GetFieldSetterStruct()
|
|
72
76
|
{
|
|
@@ -83,22 +87,24 @@
|
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
|
|
90
|
+
// TODO: Test on static fields
|
|
86
91
|
[Test]
|
|
87
92
|
public void GetFieldSetterClassGeneric()
|
|
88
93
|
{
|
|
89
94
|
TestClass testClass = new();
|
|
90
|
-
FieldSetter<TestClass, int>
|
|
95
|
+
FieldSetter<TestClass, int> classSetter = ReflectionHelpers.GetFieldSetter<
|
|
91
96
|
TestClass,
|
|
92
97
|
int
|
|
93
98
|
>(typeof(TestClass).GetField(nameof(TestClass.intValue)));
|
|
94
99
|
for (int i = 0; i < NumTries; ++i)
|
|
95
100
|
{
|
|
96
101
|
int expected = _random.Next(int.MinValue, int.MaxValue);
|
|
97
|
-
|
|
102
|
+
classSetter(ref testClass, expected);
|
|
98
103
|
Assert.AreEqual(expected, testClass.intValue);
|
|
99
104
|
}
|
|
100
105
|
}
|
|
101
106
|
|
|
107
|
+
// TODO: Test on static fields
|
|
102
108
|
[Test]
|
|
103
109
|
public void GetFieldSetterStructGeneric()
|
|
104
110
|
{
|
|
@@ -115,6 +121,36 @@
|
|
|
115
121
|
}
|
|
116
122
|
}
|
|
117
123
|
|
|
124
|
+
// TODO: Test on static fields
|
|
125
|
+
[Test]
|
|
126
|
+
public void GetFieldGetterClassGeneric()
|
|
127
|
+
{
|
|
128
|
+
TestClass testClass = new();
|
|
129
|
+
Func<TestClass, int> classGetter = ReflectionHelpers.GetFieldGetter<TestClass, int>(
|
|
130
|
+
typeof(TestClass).GetField(nameof(TestClass.intValue))
|
|
131
|
+
);
|
|
132
|
+
for (int i = 0; i < NumTries; ++i)
|
|
133
|
+
{
|
|
134
|
+
testClass.intValue = _random.Next(int.MinValue, int.MaxValue);
|
|
135
|
+
Assert.AreEqual(testClass.intValue, classGetter(testClass));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// TODO: Test on static fields
|
|
140
|
+
[Test]
|
|
141
|
+
public void GetFieldGetterStructGeneric()
|
|
142
|
+
{
|
|
143
|
+
TestStruct testStruct = new();
|
|
144
|
+
Func<TestStruct, int> structSetter = ReflectionHelpers.GetFieldGetter<TestStruct, int>(
|
|
145
|
+
typeof(TestStruct).GetField(nameof(TestStruct.intValue))
|
|
146
|
+
);
|
|
147
|
+
for (int i = 0; i < NumTries; ++i)
|
|
148
|
+
{
|
|
149
|
+
testStruct.intValue = _random.Next(int.MinValue, int.MaxValue);
|
|
150
|
+
Assert.AreEqual(testStruct.intValue, structSetter(testStruct));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
118
154
|
[Test]
|
|
119
155
|
public void ArrayCreator()
|
|
120
156
|
{
|