コード例 #1
0
    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.");
        }
    }
コード例 #2
0
    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);
    }
コード例 #3
0
ファイル: Delegate.cs プロジェクト: ForNeVeR/pnet
        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;
        }
コード例 #4
0
 //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);
 }
コード例 #5
0
 //Does this object have a method called name
 public static bool HasMethod(this System.Object obj, string name)
 {
     return(obj.GetMethod(name) != null);
 }