Guest User

Unboxing

a guest
Aug 31st, 2026
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | Source Code | 0 0
  1.     /// <summary>
  2.     /// Returns a MethodInfo of the given type by passing in the string name of it. If it is not yet unboxed, unbox it. Subsequent requests will be faster.
  3.     /// </summary>
  4.     /// <param name="t"></param>
  5.     /// <param name="methodName"></param>
  6.     /// <returns></returns>
  7.     public static MethodInfo TryGetMethod(Type t, string methodName)
  8.     {
  9.         if (!dictUnboxedMethods.TryGetValue(t, out Dictionary<string, MethodInfo> dictMethodsOfType))
  10.         {
  11.             dictUnboxedMethods[t] = new Dictionary<string, MethodInfo>();
  12.             dictMethodsOfType = dictUnboxedMethods[t];
  13.         }
  14.  
  15.         if (dictMethodsOfType.TryGetValue(methodName, out MethodInfo checkMethod))
  16.         {
  17.             return checkMethod;
  18.         }
  19.  
  20.         // We haven't unboxed the method yet, unbox it now.
  21.  
  22.         checkMethod = t.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
  23.  
  24.         if (checkMethod == null)
  25.         {
  26.             var allMethods = t.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
  27.             Debug.Log($"Available methods on {t.Name}: {string.Join(", ", allMethods.Select(m => m.Name))}");
  28.  
  29.             Debug.LogError("Could not find method " + methodName + "!");
  30.             return null;
  31.         }
  32.  
  33.         dictMethodsOfType.Add(methodName, checkMethod);
  34.         return checkMethod;
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment