コード例 #1
0
        /// <summary>
        /// Invoke the best available match for the supplied parameters.
        /// If no method can be called using the supplied parameters, an exception is thrown.
        /// </summary>
        /// <param name="obj">The object on which to invoke a method.</param>
        /// <param name="mustUseAllParameters">Specifies whether all supplied parameters must be used in the
        /// invocation. Unless you know what you are doing you should pass true for this parameter.</param>
        /// <param name="parameters">A dictionary of parameter name/value pairs.</param>
        /// <returns>The return value of the invocation.</returns>
        public object Invoke(object obj, bool mustUseAllParameters, Dictionary <string, object> parameters)
        {
            bool isStatic = obj is Type;

            string[] names  = parameters.Keys.ToArray() ?? new string[0];
            object[] values = parameters.Values.ToArray() ?? new object[0];
            Type[]   types  = values.ToTypeArray() ?? new Type[0];
            if (names.Length != values.Length || names.Length != types.Length)
            {
                throw new ArgumentException("Mismatching name, type and value arrays (must be of identical length).");
            }
            MethodMap map = MapFactory.DetermineBestMethodMatch(methodPool, mustUseAllParameters, names, types, values);

            return(isStatic ? map.Invoke(values) : map.Invoke(obj, values));
        }
コード例 #2
0
        /// <summary>
        /// Invoke the best available match for the supplied parameters.
        /// If no method can be called using the supplied parameters, an exception is thrown.
        /// </summary>
        /// <param name="obj">The object on which to invoke a method.</param>
        /// <param name="mustUseAllParameters">Specifies whether all supplied parameters must be used in the
        /// invocation. Unless you know what you are doing you should pass true for this parameter.</param>
        /// <param name="sample">The object whose public properties will be used as parameters.</param>
        /// <returns>The return value of the invocation.</returns>
        public object Invoke(object obj, bool mustUseAllParameters, object sample)
        {
            Type sourceType = sample.GetType();
            var  sourceInfo = new SourceInfo(sourceType);
            bool isStatic   = obj is Type;

            string[] names  = sourceInfo.ParamNames;
            Type[]   types  = sourceInfo.ParamTypes;
            object[] values = sourceInfo.GetParameterValues(sample);
            if (names.Length != values.Length || names.Length != types.Length)
            {
                throw new ArgumentException("Mismatching name, type and value arrays (must be of identical length).");
            }
            MethodMap map = MapFactory.DetermineBestMethodMatch(methodPool, mustUseAllParameters, names, types, values);

            return(isStatic ? map.Invoke(values) : map.Invoke(obj, values));
        }