Example #1
0
        public static MethodInfo GetMethod(
#if !NET20
            this
#endif
            Type Type, String MethodName, Int32 ParameterCount)
        {
            if (ParameterCount < 0)
            {
                ParameterCount = -1;
            }

            if (Type == null)
            {
                return(null);
            }

            lock (_methodsCache)
                if (!_methodsCache.ContainsKey(Type))
                // lock (_methodsUppercaseCache)
                //   if (!_methodsUppercaseCache.ContainsKey(Type))
                {
                    _methodsCache[Type] = new Dictionary <string, Dictionary <int, MethodInfo> >();
                    //_methodsUppercaseCache[Type] = new Dictionary<string, Dictionary<int, MethodInfo>>();

                    IList <MethodInfo> methods = Type.
                                                 GetMethods();

                    Int32 index = -1;
#if !NET20
                    foreach (MethodInfo method in methods.
                             OrderByDescending(m =>
                                               m.GetParameters().Length > 0 ?
                                               m.GetParameters()[0].ParameterType == typeof(string) ?
                                               10 : 5 : 0).
                             OrderBy(m => m.Name).
                             OrderBy(m => m.GetParameters().Length))
#else
                    foreach (MethodInfo method in Linq2.OrderBy(
                                 Linq2.OrderBy(
                                     Linq2.OrderByDescending(methods, m =>
                                                             m.GetParameters().Length > 0 ?
                                                             m.GetParameters()[0].ParameterType == typeof(string) ?
                                                             10 : 5 : 0),
                                     m => m.Name),
                                 m => m.GetParameters().Length))
#endif
                    {
                        index++;

                        if (!_methodsCache[Type].ContainsKey(method.Name))
                        {
                            _methodsCache[Type][method.Name] = new Dictionary <int, MethodInfo>();
                        }

                        //if (!_methodsUppercaseCache[Type].ContainsKey(method.Name.ToUpper()))
                        //    _methodsUppercaseCache[Type][method.Name.ToUpper()] = new Dictionary<int, MethodInfo>();

                        if (index == 0)
                        {
                            if (!_methodsCache[Type][method.Name].ContainsKey(-1))
                            {
                                _methodsCache[Type][method.Name][-1] = method;
                                //_methodsUppercaseCache[Type][method.Name.ToUpper()][-1] = method;
                            }
                        }

                        Int32 parametersCount = method.GetParameters().Length;
                        if (!_methodsCache[Type][method.Name].ContainsKey(parametersCount))
                        {
                            _methodsCache[Type][method.Name][parametersCount] = method;
                            //_methodsUppercaseCache[Type][method.Name.ToUpper()][parametersCount] = method;
                        }
                    }
                }

            Dictionary <string, Dictionary <int, MethodInfo> > dict = null;
            Dictionary <int, MethodInfo> innerDict = null;
            MethodInfo result = null;

            ////////////////////////////////////////////

            _methodsCache.TryGetValue(Type, out dict);

            if (dict != null)
            {
                dict.TryGetValue(MethodName, out innerDict);
            }

            if (innerDict != null)
            {
                if (ParameterCount == -2 && innerDict.Values.Count > 0)

#if !NET20
                { return(innerDict.Values.FirstOrDefault()); }
#else
                { return(Linq2.FirstOrDefault(innerDict.Values)); }
#endif
                innerDict.TryGetValue(ParameterCount, out result);
            }

            if (result != null)
            {
                return(result);
            }

            ////////////////////////////////////////////

            /*_methodsUppercaseCache.TryGetValue(Type, out dict);
             *
             * if (dict != null)
             *  dict.TryGetValue(MethodName.ToUpper(), out innerDict);
             *
             * if (innerDict != null)
             * {
             *  if (ParameterCount == -2 && innerDict.Values.Count > 0)
             *      return innerDict.Values.FirstOrDefault();
             *
             *  innerDict.TryGetValue(ParameterCount, out result);
             * }
             *
             * if (result != null)
             *  return result;*/

            return(null);
        }