public static void CallAction(this System.Object obj, string name, params System.Object[] parameters) { MethodInfo info = obj.GetMethod(name); if (info != null) { ParameterInfo[] signature = info.GetParameters(); if (signature.Length == parameters.Length) { for (int i = 0; i < signature.Length; i++) { if (!parameters[i].CanCastTo(signature[i].ParameterType)) { Debug.LogWarning("ReflectionF.CallAction: Function " + name + " on instance of " + obj.GetType().ShortName() + " does not match given parameters."); return; } } info.Invoke(obj, parameters); } else { Debug.LogWarning("ReflectionF.CallAction: Function " + name + " on instance of " + obj.GetType().ShortName() + " does not match given parameters."); } } else { Debug.LogWarning("ReflectionF.CallAction: Function " + name + " on instance of " + obj.GetType().ShortName() + " does not exist."); } }
public static bool HasMethod <T>(this System.Object obj, string name) { MethodInfo info = obj.GetMethod(name); if (info != null) { return(info.ReturnType == typeof(T)); } return(false); }
protected Delegate(Type target, String method) { if (target == null) { throw new ArgumentNullException("target"); } if (method == null) { throw new ArgumentNullException("method"); } this.target = null; MethodInfo methodInfo = target.GetMethod(method, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); if (methodInfo == null) { throw new MissingMethodException (_("Arg_DelegateMethod")); } this.method = methodInfo.MethodHandle; }
//Less safe than CallAction(), but faster. //Only use this when it is KNOWN that name exists, and the constructed params match the target method's signature. public static void CallActionQ(this System.Object obj, string name, params System.Object[] parameters) { obj.GetMethod(name).Invoke(obj, parameters); }
//Does this object have a method called name public static bool HasMethod(this System.Object obj, string name) { return(obj.GetMethod(name) != null); }