CallMethod() public static method

Calls a method on an object dynamically.
public static CallMethod ( object instance, string method ) : object
instance object
method string
return object
コード例 #1
0
        /// <summary>
        /// Calls a method on an object with extended . syntax (object: this Method: Entity.CalculateOrderTotal)
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="method"></param>
        /// <param name="params"></param>
        /// <returns></returns>
        public static object CallMethodEx(object parent, string method, params object[] parms)
        {
            Type Type = parent.GetType();

            // *** no more .s - we got our final object
            int lnAt = method.IndexOf(".");

            if (lnAt < 0)
            {
                return(ReflectionUtils.CallMethod(parent, method, parms));
            }

            // *** Walk the . syntax
            string Main = method.Substring(0, lnAt);
            string Subs = method.Substring(lnAt + 1);

            object Sub = GetPropertyInternal(parent, Main);

            // *** Recurse until we get the lowest ref
            return(CallMethodEx(Sub, Subs, parms));
        }
コード例 #2
0
        /// <summary>
        /// Calls a method on a COM object with '.' syntax (Customer instance and Address.DoSomeThing method)
        /// </summary>
        /// <param name="parent">the object instance on which to call method</param>
        /// <param name="method">The method or . syntax path to the method (Address.Parse)</param>
        /// <param name="parms">Any number of parameters</param>
        /// <returns></returns>
        public static object CallMethodExCom(object parent, string method, params object[] parms)
        {
            Type Type = parent.GetType();

            // no more .s - we got our final object
            int at = method.IndexOf(".");

            if (at < 0)
            {
                return(ReflectionUtils.CallMethod(parent, method, parms));
            }

            // Walk the . syntax - split into current object (Main) and further parsed objects (Subs)
            string Main = method.Substring(0, at);
            string Subs = method.Substring(at + 1);

            object Sub = parent.GetType().InvokeMember(Main, ReflectionUtils.MemberAccess | BindingFlags.GetProperty | BindingFlags.GetField, null,
                                                       parent, null);

            // Recurse until we get the lowest ref
            return(CallMethodEx(Sub, Subs, parms));
        }