Example #1
0
        /// <summary>
        /// Retrieve a specific extension method for the type that matches the function parameters.
        /// Each result gets cached, so subsequent calls are going to be much faster and won't cause any GC allocation.
        /// </summary>

        static public MethodInfo GetMethodOrExtension(this Type type, string name, params Type[] paramTypes)
        {
            Dictionary <Type, List <ExtesionType> > cachedMethod;

            if (!mCache.TryGetValue(name, out cachedMethod) || cachedMethod == null)
            {
                cachedMethod = new Dictionary <Type, List <ExtesionType> >();
                mCache.Add(name, cachedMethod);
            }

            List <ExtesionType> cachedList = null;

            if (!cachedMethod.TryGetValue(type, out cachedList) || cachedList == null)
            {
                cachedList = new List <ExtesionType>();
                cachedMethod.Add(type, cachedList);
            }

            for (int b = 0; b < cachedList.size; ++b)
            {
                var  item    = cachedList[b];
                bool isValid = true;

                if (item.paramTypes != paramTypes)
                {
                    if (item.paramTypes.Length == paramTypes.Length)
                    {
                        for (int i = 0, imax = item.paramTypes.Length; i < imax; ++i)
                        {
                            if (item.paramTypes[i] != paramTypes[i])
                            {
                                isValid = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        isValid = false;
                    }
                }
                if (isValid)
                {
                    return(item.method);
                }
            }

            var ci = new ExtesionType();

            ci.method = type.GetMethod(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, null, paramTypes, null);
            if (ci.method == null)
            {
                ci.method = type.GetExtensionMethod(name, paramTypes);
            }
            ci.paramTypes = paramTypes;
            cachedList.Add(ci);
            return(ci.method);
        }
Example #2
0
        /// <summary>
        /// Retrieve a specific extension method for the type that matches the function parameters.
        /// Each result gets cached, so subsequent calls are going to be much faster and won't cause any GC allocation.
        /// </summary>

        static public MethodInfo GetMethodOrExtension(this Type type, string name, Type paramType)
        {
            Dictionary <Type, List <ExtesionType> > cachedMethod;

            if (!mCache.TryGetValue(name, out cachedMethod) || cachedMethod == null)
            {
                cachedMethod = new Dictionary <Type, List <ExtesionType> >();
                mCache.Add(name, cachedMethod);
            }

            List <ExtesionType> cachedList = null;

            if (!cachedMethod.TryGetValue(type, out cachedList) || cachedList == null)
            {
                cachedList = new List <ExtesionType>();
                cachedMethod.Add(type, cachedList);
            }

            for (int b = 0; b < cachedList.size; ++b)
            {
                var item = cachedList[b];
                if (item.paramTypes.Length == 1 && item.paramTypes[0] == paramType)
                {
                    return(item.method);
                }
            }

            var paramTypes = new Type[] { paramType };
            var ci         = new ExtesionType();

            ci.method = type.GetMethod(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, null, paramTypes, null);
            if (ci.method == null)
            {
                ci.method = type.GetExtensionMethod(name, paramTypes);
            }
            ci.paramTypes = paramTypes;
            cachedList.Add(ci);
            return(ci.method);
        }