Exemple #1
0
        public static MethodMap PrepareInvoke(Type type, string[] paramNames, Type[] parameterTypes,
                                              object[] sampleParamValues)
        {
            SourceInfo sourceInfo = new SourceInfo(type, paramNames, parameterTypes);
            MethodMap  map        = ctorMapCache.Get(sourceInfo);

            if (map == null)
            {
                map = DetermineBestConstructorMatch(type, paramNames, parameterTypes, sampleParamValues);
                ctorMapCache.Insert(sourceInfo, map);
            }
            return(map);
        }
        /// <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)
        {
            if (obj == null || parameters == null)
            {
                throw new ArgumentException("Missing or invalid argument: " + (obj == null ? "obj" : "parameters"));
            }
            string[] names      = parameters.Keys.ToArray() ?? new string[0];
            object[] values     = parameters.Values.ToArray() ?? new object[0];
            Type[]   types      = values.ToTypeArray() ?? new Type[0];
            bool     isStatic   = obj is Type;
            var      type       = isStatic ? obj as Type : obj.GetType();
            var      sourceInfo = new SourceInfo(type, names, types);
            // check to see if we already have a map for best match
            MethodMap map = mapCache.Get(sourceInfo);

            if (map == null)
            {
                map = MapFactory.DetermineBestMethodMatch(methodPool, mustUseAllParameters, names, types, values);
                mapCache.Insert(sourceInfo, map);
            }
            return(isStatic ? map.Invoke(values) : map.Invoke(obj, values));
        }