All pastes #193488 Raw Edit

Unnamed

public text v1 · immutable
·published 2006-10-06 22:13 UTC
rendered paste body
        /// <summary>
        /// Invokes the named method of the specified target object.
        /// </summary>
        /// <param name="target">The target object</param>
        /// <param name="methodName">The method name</param>
        /// <param name="byRef">Specifies which arguments must be passed by reference.</param>
        /// <param name="args">The method arguments</param>
        /// <returns></returns>
        /// <example>
        /// <code>
        /// //
        /// // calling method string Test(string arg1, ref object arg2);
        /// //
        /// SomeObject x = new SomeObject();
        /// string s = (string) Introspect.InvokeMethod(o, "Test",
        ///     new bool[] {false, true}, new object[] {"hi!", x});
        /// </code>
        /// </example>
        public static object InvokeMethod(object target, string methodName, bool[] byRef, object[] args)
        {
            if (byRef.Length != args.Length)
                throw new ArgumentException("Length of byRef and args must match.", "byRef, args");

            ParameterModifier[] mods = new ParameterModifier[byRef.Length];

            for (int i = 0; i < byRef.Length; i++) 
            {
                ParameterModifier m = new ParameterModifier(1);
                m[0] = byRef[i];
                mods[i] = m;
            }

            return target.GetType().InvokeMember(
                methodName,
                BindingFlags.InvokeMethod,
                null,
                target,
                args,
                mods,
                null,
                null
                );
        }