Skip to content

Commit a412e39

Browse files
NH-3964 - use ReflectionHelper instead of Type.GetMethod when it is adequate.
1 parent 078f5f3 commit a412e39

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

src/NHibernate/Bytecode/EmitUtil.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Reflection;
33
using System.Reflection.Emit;
44
using System.Collections.Generic;
5+
using NHibernate.Linq;
6+
using NHibernate.Util;
57

68
namespace NHibernate.Bytecode
79
{
@@ -185,22 +187,18 @@ public static System.Type DefineDelegateType(
185187
public static void EmitLoadType(ILGenerator il, System.Type type)
186188
{
187189
il.Emit(OpCodes.Ldtoken, type);
188-
il.Emit(OpCodes.Call, typeof(System.Type).GetMethod("GetTypeFromHandle"));
190+
il.Emit(OpCodes.Call, ReflectionCache.TypeMethods.GetTypeFromHandle);
189191
}
190192

191-
private static readonly MethodInfo GetMethodFromHandle = typeof(MethodBase).GetMethod(
192-
"GetMethodFromHandle", new System.Type[] { typeof(RuntimeMethodHandle) });
193-
194193
public static void EmitLoadMethodInfo(ILGenerator il, MethodInfo methodInfo)
195194
{
196195
il.Emit(OpCodes.Ldtoken, methodInfo);
197-
il.Emit(OpCodes.Call, GetMethodFromHandle);
196+
il.Emit(OpCodes.Call, ReflectionCache.MethodBaseMethods.GetMethodFromHandle);
198197
il.Emit(OpCodes.Castclass, typeof(MethodInfo));
199198
}
200199

201-
private static readonly MethodInfo CreateDelegate = typeof(Delegate).GetMethod(
202-
"CreateDelegate", BindingFlags.Static | BindingFlags.Public | BindingFlags.ExactBinding, null,
203-
new System.Type[] { typeof(System.Type), typeof(MethodInfo) }, null);
200+
private static readonly MethodInfo CreateDelegate = ReflectionHelper.GetMethod(
201+
() => Delegate.CreateDelegate(null, null));
204202

205203
public static void EmitCreateDelegateInstance(ILGenerator il, System.Type delegateType, MethodInfo methodInfo)
206204
{

src/NHibernate/Bytecode/Lightweight/ReflectionOptimizer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reflection.Emit;
33
using System.Security;
44
using System.Security.Permissions;
5+
using NHibernate.Linq;
56
using NHibernate.Properties;
67
using NHibernate.Util;
78

@@ -118,8 +119,8 @@ private static void EmitCastToReference(ILGenerator il, System.Type type)
118119
}
119120
}
120121

121-
private static readonly MethodInfo GetterCallbackInvoke = typeof (GetterCallback).GetMethod(
122-
"Invoke", new[] { typeof (object), typeof (int) });
122+
private static readonly MethodInfo GetterCallbackInvoke = ReflectionHelper.GetMethod<GetterCallback>(
123+
g => g.Invoke(null, 0));
123124

124125
/// <summary>
125126
/// Generates a dynamic method on the given type.
@@ -184,8 +185,8 @@ private GetPropertyValuesInvoker GenerateGetPropertyValuesMethod(IGetter[] gette
184185
return (GetPropertyValuesInvoker) method.CreateDelegate(typeof (GetPropertyValuesInvoker));
185186
}
186187

187-
private static readonly MethodInfo SetterCallbackInvoke = typeof(SetterCallback).GetMethod(
188-
"Invoke", new[] { typeof(object), typeof(int), typeof(object) });
188+
private static readonly MethodInfo SetterCallbackInvoke = ReflectionHelper.GetMethod<SetterCallback>(
189+
g => g.Invoke(null, 0, null));
189190

190191
/// <summary>
191192
/// Generates a dynamic method on the given type.

src/NHibernate/Proxy/DynamicProxy/DefaultMethodEmitter.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@
1010
using System.Diagnostics;
1111
using System.Reflection;
1212
using System.Reflection.Emit;
13+
using NHibernate.Linq;
14+
using NHibernate.Util;
1315

1416
namespace NHibernate.Proxy.DynamicProxy
1517
{
1618
internal class DefaultMethodEmitter : IMethodBodyEmitter
1719
{
1820
private static readonly MethodInfo getInterceptor;
1921

20-
private static readonly MethodInfo getGenericMethodFromHandle = typeof (MethodBase).GetMethod("GetMethodFromHandle",
21-
BindingFlags.Public | BindingFlags.Static, null,
22-
new[] {typeof (RuntimeMethodHandle), typeof (RuntimeTypeHandle)}, null);
23-
24-
private static readonly MethodInfo getMethodFromHandle = typeof (MethodBase).GetMethod("GetMethodFromHandle", new[] {typeof (RuntimeMethodHandle)});
25-
private static readonly MethodInfo getTypeFromHandle = typeof(System.Type).GetMethod("GetTypeFromHandle");
26-
private static readonly MethodInfo handlerMethod = typeof (IInterceptor).GetMethod("Intercept");
22+
private static readonly MethodInfo handlerMethod = ReflectionHelper.GetMethod<IInterceptor>(
23+
i => i.Intercept(null));
2724
private static readonly MethodInfo getArguments = typeof(InvocationInfo).GetMethod("get_Arguments");
2825

2926
private static readonly ConstructorInfo infoConstructor = typeof (InvocationInfo).GetConstructor(new[]
@@ -192,11 +189,11 @@ private static void PushTargetMethodInfo(ILGenerator IL, MethodBuilder generated
192189
if (declaringType.IsGenericType)
193190
{
194191
IL.Emit(OpCodes.Ldtoken, declaringType);
195-
IL.Emit(OpCodes.Call, getGenericMethodFromHandle);
192+
IL.Emit(OpCodes.Call, ReflectionCache.MethodBaseMethods.GetMethodFromHandleWithDeclaringType);
196193
}
197194
else
198195
{
199-
IL.Emit(OpCodes.Call, getMethodFromHandle);
196+
IL.Emit(OpCodes.Call, ReflectionCache.MethodBaseMethods.GetMethodFromHandle);
200197
}
201198

202199
IL.Emit(OpCodes.Castclass, typeof(MethodInfo));
@@ -232,7 +229,7 @@ private void PushGenericArguments(MethodInfo method, ILGenerator IL)
232229
IL.Emit(OpCodes.Dup);
233230
IL.Emit(OpCodes.Ldc_I4, index);
234231
IL.Emit(OpCodes.Ldtoken, currentType);
235-
IL.Emit(OpCodes.Call, getTypeFromHandle);
232+
IL.Emit(OpCodes.Call, ReflectionCache.TypeMethods.GetTypeFromHandle);
236233
IL.Emit(OpCodes.Stelem_Ref);
237234
}
238235
}

src/NHibernate/Proxy/DynamicProxy/ProxyFactory.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
using System.Reflection;
1313
using System.Reflection.Emit;
1414
using System.Runtime.Serialization;
15+
using NHibernate.Linq;
16+
using NHibernate.Util;
1517

1618
namespace NHibernate.Proxy.DynamicProxy
1719
{
1820
public sealed class ProxyFactory
1921
{
2022
private static readonly ConstructorInfo defaultBaseConstructor = typeof(object).GetConstructor(new System.Type[0]);
21-
private static readonly MethodInfo getTypeFromHandle = typeof(System.Type).GetMethod("GetTypeFromHandle");
2223

23-
private static readonly MethodInfo getValue = typeof (SerializationInfo).GetMethod("GetValue", BindingFlags.Public | BindingFlags.Instance, null,
24-
new[] { typeof(string), typeof(System.Type) }, null);
25-
26-
private static readonly MethodInfo setType = typeof(SerializationInfo).GetMethod("SetType", BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(System.Type) }, null);
27-
28-
private static readonly MethodInfo addValue = typeof (SerializationInfo).GetMethod("AddValue", BindingFlags.Public | BindingFlags.Instance, null,
29-
new[] {typeof (string), typeof (object)}, null);
24+
private static readonly MethodInfo getValue = ReflectionHelper.GetMethod<SerializationInfo>(
25+
si => si.GetValue(null, null));
26+
private static readonly MethodInfo setType = ReflectionHelper.GetMethod<SerializationInfo>(
27+
si => si.SetType(null));
28+
private static readonly MethodInfo addValue = ReflectionHelper.GetMethod<SerializationInfo>(
29+
si => si.AddValue(null, null));
3030

3131
public ProxyFactory()
3232
: this(new DefaultyProxyMethodBuilder()) {}
@@ -221,7 +221,7 @@ private static void ImplementGetObjectData(System.Type baseType, System.Type[] b
221221
// info.SetType(typeof(ProxyObjectReference));
222222
IL.Emit(OpCodes.Ldarg_1);
223223
IL.Emit(OpCodes.Ldtoken, typeof (ProxyObjectReference));
224-
IL.Emit(OpCodes.Call, getTypeFromHandle);
224+
IL.Emit(OpCodes.Call, ReflectionCache.TypeMethods.GetTypeFromHandle);
225225
IL.Emit(OpCodes.Callvirt, setType);
226226

227227
// info.AddValue("__interceptor", __interceptor);
@@ -276,7 +276,7 @@ private static void DefineSerializationConstructor(TypeBuilder typeBuilder, Fiel
276276

277277

278278
IL.Emit(OpCodes.Ldtoken, typeof (IInterceptor));
279-
IL.Emit(OpCodes.Call, getTypeFromHandle);
279+
IL.Emit(OpCodes.Call, ReflectionCache.TypeMethods.GetTypeFromHandle);
280280
IL.Emit(OpCodes.Stloc, interceptorType);
281281

282282
IL.Emit(OpCodes.Ldarg_0);

src/NHibernate/Util/ReflectionCache.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal static class ReflectionCache
1616
// - If the method is generic, suffix it with "On" followed by its generic parameter type names.
1717
// Avoid caching here narrow cases, such as those using specific types and unlikely to be used by many classes.
1818
// Cache them instead in classes using them.
19+
1920
internal static class EnumerableMethods
2021
{
2122
internal static readonly MethodInfo AggregateDefinition =
@@ -40,5 +41,19 @@ internal static class EnumerableMethods
4041
internal static readonly MethodInfo ToListDefinition =
4142
ReflectionHelper.GetMethodDefinition(() => Enumerable.ToList<object>(null));
4243
}
44+
45+
internal static class MethodBaseMethods
46+
{
47+
internal static readonly MethodInfo GetMethodFromHandle =
48+
ReflectionHelper.GetMethod(() => MethodBase.GetMethodFromHandle(new RuntimeMethodHandle()));
49+
internal static readonly MethodInfo GetMethodFromHandleWithDeclaringType =
50+
ReflectionHelper.GetMethod(() => MethodBase.GetMethodFromHandle(new RuntimeMethodHandle(), new RuntimeTypeHandle()));
51+
}
52+
53+
internal static class TypeMethods
54+
{
55+
internal static readonly MethodInfo GetTypeFromHandle =
56+
ReflectionHelper.GetMethod(() => System.Type.GetTypeFromHandle(new RuntimeTypeHandle()));
57+
}
4358
}
4459
}

0 commit comments

Comments
 (0)