9using System.Collections.Generic;
10using System.ComponentModel;
11using System.Reflection;
12using System.Globalization;
13using System.Threading.Tasks;
17using System.Runtime.Loader;
26 public static class MethodCaller
28 private const BindingFlags allLevelFlags
29 = BindingFlags.FlattenHierarchy
30 | BindingFlags.Instance
32 | BindingFlags.NonPublic
35 private const BindingFlags oneLevelFlags
36 = BindingFlags.DeclaredOnly
37 | BindingFlags.Instance
39 | BindingFlags.NonPublic
42 private const BindingFlags ctorFlags
43 = BindingFlags.Instance
45 | BindingFlags.NonPublic
48 private const BindingFlags factoryFlags =
51 BindingFlags.FlattenHierarchy;
53 private const BindingFlags privateMethodFlags =
55 BindingFlags.NonPublic |
56 BindingFlags.Instance |
57 BindingFlags.FlattenHierarchy;
59 #region Dynamic Method Cache
62 private static readonly Dictionary<MethodCacheKey, Tuple<string, DynamicMethodHandle>> _methodCache =
63 new Dictionary<MethodCacheKey, Tuple<string, DynamicMethodHandle>>();
65 private readonly
static Dictionary<MethodCacheKey, DynamicMethodHandle> _methodCache =
new Dictionary<MethodCacheKey, DynamicMethodHandle>();
68 private static DynamicMethodHandle GetCachedMethod(
object obj, System.Reflection.MethodInfo info, params
object[] parameters)
70 var objectType = obj.GetType();
74 DynamicMethodHandle mh =
null;
77 var key =
new MethodCacheKey(objectType.FullName, info.Name, GetParameterTypes(parameters));
81 found = _methodCache.TryGetValue(key, out var methodHandleInfo);
83 mh = methodHandleInfo?.Item2;
92 found = _methodCache.TryGetValue(key, out var methodHandleInfo);
94 mh = methodHandleInfo?.Item2;
98 mh =
new DynamicMethodHandle(info, parameters);
100 var cacheInstance = AssemblyLoadContextManager.CreateCacheInstance(objectType, mh, OnMethodAssemblyLoadContextUnload);
102 _methodCache.Add(key, cacheInstance);
107 var key =
new MethodCacheKey(objectType.FullName, info.Name, GetParameterTypes(parameters));
111 found = _methodCache.TryGetValue(key, out mh);
120 if (!_methodCache.TryGetValue(key, out mh))
122 mh =
new DynamicMethodHandle(info, parameters);
124 _methodCache.Add(key, mh);
133 private static DynamicMethodHandle GetCachedMethod(
object obj,
string method, params
object[] parameters)
135 return GetCachedMethod(obj, method,
true, parameters);
138 private static DynamicMethodHandle GetCachedMethod(
object obj,
string method,
bool hasParameters, params
object[] parameters)
141 var objectType = obj.GetType();
143 var key =
new MethodCacheKey(objectType.FullName, method, GetParameterTypes(hasParameters, parameters));
145 DynamicMethodHandle mh;
147 var found = _methodCache.TryGetValue(key, out var methodHandleInfo);
149 mh = methodHandleInfo?.Item2;
155 found = _methodCache.TryGetValue(key, out methodHandleInfo);
157 mh = methodHandleInfo?.Item2;
161 var info = GetMethod(obj.GetType(), method, hasParameters, parameters);
163 mh =
new DynamicMethodHandle(info, parameters);
165 var cacheInstance = AssemblyLoadContextManager.CreateCacheInstance(objectType, mh, OnMethodAssemblyLoadContextUnload);
167 _methodCache.Add(key, cacheInstance);
172 var key =
new MethodCacheKey(obj.GetType().FullName, method, GetParameterTypes(hasParameters, parameters));
174 if (!_methodCache.TryGetValue(key, out DynamicMethodHandle mh))
178 if (!_methodCache.TryGetValue(key, out mh))
180 var info = GetMethod(obj.GetType(), method, hasParameters, parameters);
182 mh =
new DynamicMethodHandle(info, parameters);
184 _methodCache.Add(key, mh);
195 #region Dynamic Constructor Cache
197 private readonly
static Dictionary<Type, DynamicCtorDelegate> _ctorCache =
new Dictionary<Type, DynamicCtorDelegate>();
201 if (objectType ==
null)
202 throw new ArgumentNullException(nameof(objectType));
207 found = _ctorCache.TryGetValue(objectType, out result);
215 if (!_ctorCache.TryGetValue(objectType, out result))
217 ConstructorInfo info = objectType.GetConstructor(ctorFlags,
null, Type.EmptyTypes,
null);
219 throw new NotSupportedException(
string.Format(
220 CultureInfo.CurrentCulture,
221 "Cannot create instance of Type '{0}'. No public parameterless constructor found.",
224 result = DynamicMethodHandlerFactory.CreateConstructor(info);
225 _ctorCache.Add(objectType, result);
242 public static Type GetType(
string typeName,
bool throwOnError,
bool ignoreCase)
246 return Type.GetType(typeName, throwOnError, ignoreCase);
251 string[] splitName = typeName.Split(
',');
253 if (splitName.Length > 2)
255 var
asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(AppContext.BaseDirectory + splitName[1].Trim() +
".dll");
257 return asm.GetType(splitName[0].Trim());
274 public static Type GetType(
string typeName,
bool throwOnError)
276 return GetType(typeName, throwOnError,
false);
283 public static Type GetType(
string typeName)
285 return GetType(typeName,
true,
false);
290 private const BindingFlags propertyFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
291 private const BindingFlags fieldFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
294 private static readonly Dictionary<MethodCacheKey, Tuple<string, DynamicMemberHandle>> _memberCache =
295 new Dictionary<MethodCacheKey, Tuple<string, DynamicMemberHandle>>();
297 private static readonly Dictionary<MethodCacheKey, DynamicMemberHandle> _memberCache =
new Dictionary<MethodCacheKey, DynamicMemberHandle>();
300 internal static DynamicMemberHandle GetCachedProperty(Type objectType,
string propertyName)
302 var key =
new MethodCacheKey(objectType.FullName, propertyName, GetParameterTypes(
null));
305 var found = _memberCache.TryGetValue(key, out var memberHandleInfo);
307 var mh = memberHandleInfo?.Item2;
313 found = _memberCache.TryGetValue(key, out memberHandleInfo);
315 mh = memberHandleInfo?.Item2;
319 var info = objectType.GetProperty(propertyName, propertyFlags);
324 mh =
new DynamicMemberHandle(info);
326 var cacheInstance = AssemblyLoadContextManager.CreateCacheInstance(objectType, mh, OnMemberAssemblyLoadContextUnload);
328 _memberCache.Add(key, cacheInstance);
333 if (!_memberCache.TryGetValue(key, out DynamicMemberHandle mh))
337 if (!_memberCache.TryGetValue(key, out mh))
339 var info = objectType.GetProperty(propertyName, propertyFlags);
344 mh =
new DynamicMemberHandle(info);
346 _memberCache.Add(key, mh);
355 internal static DynamicMemberHandle GetCachedField(Type objectType,
string fieldName)
357 var key =
new MethodCacheKey(objectType.FullName, fieldName, GetParameterTypes(
null));
360 var found = _memberCache.TryGetValue(key, out var memberHandleInfo);
362 var mh = memberHandleInfo?.Item2;
368 found = _memberCache.TryGetValue(key, out memberHandleInfo);
370 mh = memberHandleInfo?.Item2;
374 var info = objectType.GetField(fieldName, fieldFlags);
379 mh =
new DynamicMemberHandle(info);
381 var cacheInstance = AssemblyLoadContextManager.CreateCacheInstance(objectType, mh, OnMemberAssemblyLoadContextUnload);
383 _memberCache.Add(key, cacheInstance);
388 if (!_memberCache.TryGetValue(key, out DynamicMemberHandle mh))
392 if (!_memberCache.TryGetValue(key, out mh))
394 var info = objectType.GetField(fieldName, fieldFlags);
399 mh =
new DynamicMemberHandle(info);
401 _memberCache.Add(key, mh);
417 public static object CallPropertyGetter(
object obj,
string property)
419 if (ApplicationContext.UseReflectionFallback)
421 var propertyInfo = obj.GetType().GetProperty(property);
422 return propertyInfo.GetValue(obj);
427 throw new ArgumentNullException(
"obj");
428 if (
string.IsNullOrEmpty(property))
429 throw new ArgumentException(
"Argument is null or empty.",
"property");
431 var mh = GetCachedProperty(obj.GetType(), property);
432 if (mh.DynamicMemberGet ==
null)
434 throw new NotSupportedException(
string.Format(
435 CultureInfo.CurrentCulture,
436 "The property '{0}' on Type '{1}' does not have a public getter.",
441 return mh.DynamicMemberGet(obj);
452 public static void CallPropertySetter(
object obj,
string property,
object value)
455 throw new ArgumentNullException(
"obj");
456 if (
string.IsNullOrEmpty(property))
457 throw new ArgumentException(
"Argument is null or empty.",
"property");
459 if (ApplicationContext.UseReflectionFallback)
461 var propertyInfo = obj.GetType().GetProperty(property);
462 propertyInfo.SetValue(obj, value);
466 var mh = GetCachedProperty(obj.GetType(), property);
467 if (mh.DynamicMemberSet ==
null)
469 throw new NotSupportedException(
string.Format(
470 CultureInfo.CurrentCulture,
471 "The property '{0}' on Type '{1}' does not have a public setter.",
476 mh.DynamicMemberSet(obj, value);
493 public static object CallMethodIfImplemented(
object obj,
string method)
495 return CallMethodIfImplemented(obj, method,
false,
null);
511 public static object CallMethodIfImplemented(
object obj,
string method, params
object[] parameters)
513 return CallMethodIfImplemented(obj, method,
true, parameters);
516 private static object CallMethodIfImplemented(
object obj,
string method,
bool hasParameters, params
object[] parameters)
518 if (ApplicationContext.UseReflectionFallback)
520 var found = (FindMethod(obj.GetType(), method, GetParameterTypes(hasParameters, parameters)) !=
null);
522 return CallMethod(obj, method, parameters);
528 var mh = GetCachedMethod(obj, method, parameters);
529 if (mh ==
null || mh.DynamicMethod ==
null)
531 return CallMethod(obj, mh, hasParameters, parameters);
542 public static bool IsMethodImplemented(
object obj,
string method, params
object[] parameters)
544 var mh = GetCachedMethod(obj, method, parameters);
545 return mh !=
null && mh.DynamicMethod !=
null;
559 public static object CallMethod(
object obj,
string method)
561 return CallMethod(obj, method,
false,
null);
578 public static object CallMethod(
object obj,
string method, params
object[] parameters)
580 return CallMethod(obj, method,
true, parameters);
583 private static object CallMethod(
object obj,
string method,
bool hasParameters, params
object[] parameters)
585 if (ApplicationContext.UseReflectionFallback)
587 System.Reflection.MethodInfo info = GetMethod(obj.GetType(), method, hasParameters, parameters);
591 return CallMethod(obj, info, hasParameters, parameters);
595 var mh = GetCachedMethod(obj, method, hasParameters, parameters);
596 if (mh ==
null || mh.DynamicMethod ==
null)
598 return CallMethod(obj, mh, hasParameters, parameters);
616 public static object CallMethod(
object obj, System.Reflection.MethodInfo info, params
object[] parameters)
618 return CallMethod(obj, info,
true, parameters);
621 private static object CallMethod(
object obj, System.Reflection.MethodInfo info,
bool hasParameters, params
object[] parameters)
623 if (ApplicationContext.UseReflectionFallback)
625 var infoParams = info.GetParameters();
626 var infoParamsCount = infoParams.Length;
627 bool hasParamArray = infoParamsCount > 0 && infoParams[infoParamsCount - 1].GetCustomAttributes(typeof(ParamArrayAttribute),
true).Length > 0;
628 bool specialParamArray =
false;
629 if (hasParamArray && infoParams[infoParamsCount - 1].ParameterType.Equals(typeof(
string[])))
630 specialParamArray =
true;
631 if (hasParamArray && infoParams[infoParamsCount - 1].ParameterType.Equals(typeof(
object[])))
632 specialParamArray =
true;
634 if (infoParamsCount == 1 && specialParamArray)
636 par =
new object[] { parameters };
638 else if (infoParamsCount > 1 && hasParamArray && specialParamArray)
640 par =
new object[infoParamsCount];
641 for (
int i = 0; i < infoParamsCount - 1; i++)
642 par[i] = parameters[i];
643 par[infoParamsCount - 1] = parameters[infoParamsCount - 1];
653 result = info.Invoke(obj, par);
658 if (e.InnerException ==
null)
661 inner = e.InnerException;
668 var mh = GetCachedMethod(obj, info, parameters);
669 if (mh ==
null || mh.DynamicMethod ==
null)
671 return CallMethod(obj, mh, hasParameters, parameters);
675 private static object CallMethod(
object obj, DynamicMethodHandle methodHandle,
bool hasParameters, params
object[] parameters)
677 object result =
null;
678 var method = methodHandle.DynamicMethod;
681 if (parameters ==
null)
682 inParams =
new object[] {
null };
684 inParams = parameters;
686 if (methodHandle.HasFinalArrayParam)
689 var pCount = methodHandle.MethodParamsLength;
690 var inCount = inParams.Length;
691 if (inCount == pCount - 1)
695 object[] paramList =
new object[pCount];
696 for (var pos = 0; pos <= pCount - 2; pos++)
697 paramList[pos] = parameters[pos];
698 paramList[paramList.Length - 1] = hasParameters && inParams.Length == 0 ? inParams :
null;
701 inParams = paramList;
703 else if ((inCount == pCount && inParams[inCount - 1] !=
null && !inParams[inCount - 1].GetType().IsArray) || inCount > pCount)
707 var extras = inParams.Length - (pCount - 1);
708 object[] extraArray = GetExtrasArray(extras, methodHandle.FinalArrayElementType);
709 Array.Copy(inParams, pCount - 1, extraArray, 0, extras);
712 object[] paramList =
new object[pCount];
713 for (var pos = 0; pos <= pCount - 2; pos++)
714 paramList[pos] = parameters[pos];
715 paramList[paramList.Length - 1] = extraArray;
718 inParams = paramList;
723 result = methodHandle.DynamicMethod(obj, inParams);
727 throw new CallMethodException(obj.GetType().Name +
"." + methodHandle.MethodName +
" " +
Resources.
MethodCallFailed, ex);
732 private static object[] GetExtrasArray(
int count, Type arrayType)
734 return (
object[])(System.Array.CreateInstance(arrayType.GetElementType(), count));
738#region Get/Find Method
750 public static System.Reflection.MethodInfo GetMethod(Type objectType,
string method)
752 return GetMethod(objectType, method,
true,
false,
null);
768 public static System.Reflection.MethodInfo GetMethod(Type objectType,
string method, params
object[] parameters)
770 return GetMethod(objectType, method,
true, parameters);
773 private static System.Reflection.MethodInfo GetMethod(Type objectType,
string method,
bool hasParameters, params
object[] parameters)
775 System.Reflection.MethodInfo result;
779 inParams =
new object[] { };
780 else if (parameters ==
null)
781 inParams =
new object[] {
null };
783 inParams = parameters;
789 result = FindMethod(objectType, method, GetParameterTypes(hasParameters, inParams));
797 result = FindMethod(objectType, method, inParams.Length);
799 catch (AmbiguousMatchException)
802 result = FindMethodUsingFuzzyMatching(objectType, method, inParams);
809 result = objectType.GetMethod(method, allLevelFlags);
814 private static System.Reflection.MethodInfo FindMethodUsingFuzzyMatching(Type objectType,
string method,
object[] parameters)
816 System.Reflection.MethodInfo result =
null;
817 Type currentType = objectType;
820 System.Reflection.MethodInfo[] methods = currentType.GetMethods(oneLevelFlags);
821 int parameterCount = parameters.Length;
823 foreach (System.Reflection.MethodInfo m in methods)
825 if (m.Name == method)
827 var infoParams = m.GetParameters();
828 var pCount = infoParams.Length;
831 if (pCount == 1 && infoParams[0].ParameterType.IsArray)
834 if (parameters.GetType().Equals(infoParams[0].ParameterType))
841 if (infoParams[pCount - 1].GetCustomAttributes(typeof(ParamArrayAttribute),
true).Length > 0)
844 if (parameterCount == pCount && parameters[pCount - 1].GetType().Equals(infoParams[pCount - 1].ParameterType))
857 foreach (System.Reflection.MethodInfo m in methods)
859 if (m.Name == method && m.GetParameters().Length == parameterCount)
868 currentType = currentType.BaseType;
869 }
while (currentType !=
null);
890 public static System.Reflection.MethodInfo FindMethod(Type objectType,
string method, Type[] types)
892 System.Reflection.MethodInfo info;
896 info = objectType.GetMethod(method, oneLevelFlags,
null, types,
null);
902 objectType = objectType.BaseType;
903 }
while (objectType !=
null);
922 public static System.Reflection.MethodInfo FindMethod(Type objectType,
string method,
int parameterCount)
927 System.Reflection.MethodInfo result =
null;
928 Type currentType = objectType;
931 System.Reflection.MethodInfo info = currentType.GetMethod(method, oneLevelFlags);
934 var infoParams = info.GetParameters();
935 var pCount = infoParams.Length;
937 ((pCount == 1 && infoParams[0].ParameterType.IsArray) ||
938 (infoParams[pCount - 1].GetCustomAttributes(typeof(ParamArrayAttribute),
true).Length > 0)))
941 if (parameterCount >= pCount - 1)
948 else if (pCount == parameterCount)
955 currentType = currentType.BaseType;
956 }
while (currentType !=
null);
967 public static Type[] GetParameterTypes()
969 return GetParameterTypes(
false,
null);
979 public static Type[] GetParameterTypes(
object[] parameters)
981 return GetParameterTypes(
true, parameters);
984 private static Type[] GetParameterTypes(
bool hasParameters,
object[] parameters)
987 return new Type[] { };
989 List<Type> result =
new List<Type>();
991 if (parameters ==
null)
993 result.Add(typeof(
object));
998 foreach (
object item
in parameters)
1002 result.Add(typeof(
object));
1006 result.Add(item.GetType());
1010 return result.ToArray();
1018 public static PropertyDescriptor GetPropertyDescriptor(Type t,
string propertyName)
1020 var propertyDescriptors = TypeDescriptor.GetProperties(t);
1021 PropertyDescriptor result =
null;
1022 foreach (PropertyDescriptor desc
in propertyDescriptors)
1023 if (desc.Name == propertyName)
1036 public static PropertyInfo GetProperty(Type objectType,
string propertyName)
1038 return objectType.GetProperty(propertyName, propertyFlags);
1047 public static object GetPropertyValue(
object obj, PropertyInfo info)
1052 result = info.GetValue(obj,
null);
1057 if (e.InnerException ==
null)
1060 inner = e.InnerException;
1072 public static object CallMethod(
object obj, System.Reflection.MethodInfo info)
1077 result = info.Invoke(obj,
null);
1082 if (e.InnerException ==
null)
1085 inner = e.InnerException;
1105 public async
static Task<object> CallMethodTryAsync(
object obj,
string method, params
object[] parameters)
1107 return await CallMethodTryAsync(obj, method,
true, parameters);
1118 public async
static Task<object> CallMethodTryAsync(
object obj,
string method)
1120 return await CallMethodTryAsync(obj, method,
false,
null);
1123 private async
static Task<object> CallMethodTryAsync(
object obj,
string method,
bool hasParameters, params
object[] parameters)
1127 if (ApplicationContext.UseReflectionFallback)
1129 var info = FindMethod(obj.GetType(), method, GetParameterTypes(hasParameters, parameters));
1132 var isAsyncTask = (info.ReturnType == typeof(Task));
1133 var isAsyncTaskObject = (info.ReturnType.IsGenericType && (info.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)));
1136 await (Task)CallMethod(obj, method, hasParameters, parameters);
1139 else if (isAsyncTaskObject)
1141 return await (Task<object>)CallMethod(obj, method, hasParameters, parameters);
1145 return CallMethod(obj, method, hasParameters, parameters);
1150 var mh = GetCachedMethod(obj, method, hasParameters, parameters);
1151 if (mh ==
null || mh.DynamicMethod ==
null)
1155 await (Task)CallMethod(obj, mh, hasParameters, parameters);
1158 else if (mh.IsAsyncTaskObject)
1160 return await (Task<object>)CallMethod(obj, mh, hasParameters, parameters);
1164 return CallMethod(obj, mh, hasParameters, parameters);
1168 catch (InvalidCastException ex)
1170 throw new NotSupportedException(
1181 public static bool IsAsyncMethod(
object obj,
string method)
1183 return IsAsyncMethod(obj, method,
false,
null);
1194 public static bool IsAsyncMethod(
object obj,
string method, params
object[] parameters)
1196 return IsAsyncMethod(obj, method,
true, parameters);
1199 internal static bool IsAsyncMethod(System.Reflection.MethodInfo info)
1201 var isAsyncTask = (info.ReturnType == typeof(Task));
1202 var isAsyncTaskObject = (info.ReturnType.IsGenericType && (info.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)));
1203 return isAsyncTask || isAsyncTaskObject;
1206 private static bool IsAsyncMethod(
object obj,
string method,
bool hasParameters, params
object[] parameters)
1208 if (ApplicationContext.UseReflectionFallback)
1210 var info = FindMethod(obj.GetType(), method, GetParameterTypes(hasParameters, parameters));
1213 return IsAsyncMethod(info);
1217 var mh = GetCachedMethod(obj, method, hasParameters, parameters);
1218 if (mh ==
null || mh.DynamicMethod ==
null)
1221 return mh.IsAsyncTask || mh.IsAsyncTaskObject;
1234 public static Task<object> CallGenericStaticMethodAsync(Type objectType,
string method, Type[] typeParams,
bool hasParameters, params
object[] parameters)
1236 var tcs =
new TaskCompletionSource<object>();
1242 var pTypes = GetParameterTypes(parameters);
1243 var methodReference = objectType.GetMethod(method, BindingFlags.Static | BindingFlags.Public,
null, CallingConventions.Any, pTypes,
null);
1244 if (methodReference ==
null)
1245 methodReference = objectType.GetMethod(method, BindingFlags.Static | BindingFlags.Public);
1246 if (methodReference ==
null)
1247 throw new InvalidOperationException(objectType.Name +
"." + method);
1248 var gr = methodReference.MakeGenericMethod(typeParams);
1249 task = (Task)gr.Invoke(
null, parameters);
1253 var methodReference = objectType.GetMethod(method, BindingFlags.Static | BindingFlags.Public,
null, CallingConventions.Any, System.Type.EmptyTypes,
null);
1254 var gr = methodReference.MakeGenericMethod(typeParams);
1255 task = (Task)gr.Invoke(
null,
null);
1258 if (task.Exception !=
null)
1259 tcs.SetException(task.Exception);
1263 catch (Exception ex)
1265 tcs.SetException(ex);
1279 public static object CallGenericMethod(
object target,
string method, Type[] typeParams,
bool hasParameters, params
object[] parameters)
1281 var objectType = target.GetType();
1285 var pTypes = GetParameterTypes(parameters);
1286 var methodReference = objectType.GetMethod(method, BindingFlags.Instance | BindingFlags.Public,
null, CallingConventions.Any, pTypes,
null);
1287 if (methodReference ==
null)
1288 methodReference = objectType.GetMethod(method, BindingFlags.Instance | BindingFlags.Public);
1289 if (methodReference ==
null)
1290 throw new InvalidOperationException(objectType.Name +
"." + method);
1291 var gr = methodReference.MakeGenericMethod(typeParams);
1292 result = gr.Invoke(target, parameters);
1296 var methodReference = objectType.GetMethod(method, BindingFlags.Static | BindingFlags.Public,
null, CallingConventions.Any, System.Type.EmptyTypes,
null);
1297 if (methodReference ==
null)
1298 throw new InvalidOperationException(objectType.Name +
"." + method);
1299 var gr = methodReference.MakeGenericMethod(typeParams);
1300 result = gr.Invoke(target,
null);
1312 public static object CallFactoryMethod(Type objectType,
string method, params
object[] parameters)
1315 System.Reflection.MethodInfo factory = objectType.GetMethod(
1316 method, factoryFlags,
null,
1317 MethodCaller.GetParameterTypes(parameters),
null);
1319 if (factory ==
null)
1324 int parameterCount = parameters.Length;
1325 System.Reflection.MethodInfo[] methods = objectType.GetMethods(factoryFlags);
1326 foreach (System.Reflection.MethodInfo oneMethod in methods)
1327 if (oneMethod.Name == method && oneMethod.GetParameters().Length == parameterCount)
1329 factory = oneMethod;
1333 if (factory ==
null)
1337 throw new InvalidOperationException(
1342 returnValue = factory.Invoke(
null, parameters);
1344 catch (Exception ex)
1347 if (ex.InnerException ==
null)
1350 inner = ex.InnerException;
1362 public static System.Reflection.MethodInfo GetNonPublicMethod(Type objectType,
string method)
1364 var result = FindMethod(objectType, method, privateMethodFlags);
1375 public static System.Reflection.MethodInfo FindMethod(Type objType,
string method, BindingFlags flags)
1377 System.Reflection.MethodInfo info;
1381 info = objType.GetMethod(method, flags);
1384 objType = objType.BaseType;
1385 }
while (objType !=
null);
1389#if NET5_0_OR_GREATER
1391 private static void OnMethodAssemblyLoadContextUnload(AssemblyLoadContext context)
1394 AssemblyLoadContextManager.RemoveFromCache(_methodCache, context);
1397 private static void OnMemberAssemblyLoadContextUnload(AssemblyLoadContext context)
1400 AssemblyLoadContextManager.RemoveFromCache(_memberCache, context);
A strongly-typed resource class, for looking up localized strings, etc.
static string MethodNotImplemented
Looks up a localized string similar to not implemented.
static string MemberNotFoundException
Looks up a localized string similar to Member not found on object ({0}).
static string MethodCallFailed
Looks up a localized string similar to method call failed.
static string NoSuchFactoryMethod
Looks up a localized string similar to No such factory method:{0}.
static string TaskOfObjectException
Looks up a localized string similar to Method {0} must return Task<object>.
delegate object DynamicCtorDelegate()
Delegate for a dynamic constructor method.