Пример #1
0
        public static MemberInfo ConvertToInterfaceMI(MemberInfo mi)
        {
            MemberInfo info = (MemberInfo)Cache.Get(mi);

            if (info != null)
            {
                return(info);
            }
            MethodInfo info2 = mi as MethodInfo;

            if (info2 == null)
            {
                return(null);
            }
            MethodInfo nv            = null;
            Type       reflectedType = info2.ReflectedType;

            if (reflectedType.IsInterface)
            {
                nv = info2;
            }
            else
            {
                Type[] interfaces = reflectedType.GetInterfaces();
                if (interfaces == null)
                {
                    return(null);
                }
                for (int i = 0; i < interfaces.Length; i++)
                {
                    InterfaceMapping interfaceMap = reflectedType.GetInterfaceMap(interfaces[i]);
                    if (interfaceMap.TargetMethods != null)
                    {
                        for (int j = 0; j < interfaceMap.TargetMethods.Length; j++)
                        {
                            if (interfaceMap.TargetMethods[j] == info2)
                            {
                                nv = interfaceMap.InterfaceMethods[j];
                                break;
                            }
                        }
                        if (nv != null)
                        {
                            break;
                        }
                    }
                }
            }
            Cache.Reset(mi, nv);
            return(nv);
        }
Пример #2
0
        public static MemberInfo ConvertToClassMI(Type t, MemberInfo mi)
        {
            Type reflectedType = mi.ReflectedType;

            if (!reflectedType.IsInterface)
            {
                return(mi);
            }
            Cachetable cachetable = (Cachetable)Cache.Get(t);

            if (cachetable != null)
            {
                MemberInfo info = (MemberInfo)cachetable.Get(mi);
                if (info != null)
                {
                    return(info);
                }
            }
            MethodInfo       info2        = (MethodInfo)mi;
            MethodInfo       nv           = null;
            InterfaceMapping interfaceMap = t.GetInterfaceMap(reflectedType);

            if (interfaceMap.TargetMethods == null)
            {
                throw new InvalidCastException();
            }
            for (int i = 0; i < interfaceMap.TargetMethods.Length; i++)
            {
                if (interfaceMap.InterfaceMethods[i] == info2)
                {
                    nv = interfaceMap.TargetMethods[i];
                    break;
                }
            }
            if (cachetable == null)
            {
                cachetable = (Cachetable)Cache.Set(t, new Cachetable());
            }
            cachetable.Reset(mi, nv);
            return(nv);
        }
Пример #3
0
        public static MemberInfo ConvertToClassMI(Type t, MemberInfo mi)
        {
            // get reflected type
            Type reflectType = mi.ReflectedType;

            if (!reflectType.IsInterface)
            {
                return(mi);
            }

            // First, try to hit the cache.  We look for the cache entry
            // for this type, and it should be a little cache-table
            // of it's own.  In that cache-table, we
            Cachetable subcache = (Cachetable)Cache.Get(t);

            if (subcache != null)
            {
                MemberInfo cmi = (MemberInfo)subcache.Get(mi);
                if (cmi != null)
                {
                    return(cmi);
                }
            }

            DBG.Assert(t != null, "class type is null");
            DBG.Assert(!t.IsInterface, " class type is actually an interface");

            MethodInfo minfo         = (MethodInfo)mi;
            MethodInfo clsMethodInfo = null;

            // minfo is an interface member info, map it to class memeber info

            // get the interface map
            DBG.Info(DBG.SC, "ReflectionCache: Looking up " + reflectType + " on " + t);
            InterfaceMapping imap = t.GetInterfaceMap(reflectType);

            if (imap.TargetMethods == null)
            {
                throw new InvalidCastException();
            }
            for (int i = 0; i < imap.TargetMethods.Length; i++)
            {
                if (imap.InterfaceMethods[i] == minfo)
                {
                    clsMethodInfo = imap.TargetMethods[i];
                    break;
                }
            }

            DBG.Assert(clsMethodInfo != null, "Failed to map interface method to class method");
            DBG.Assert(!clsMethodInfo.ReflectedType.IsInterface,
                       "Failed to map interface method to class method");

            // Store the result in the cache:
            if (subcache == null)
            {
                subcache = (Cachetable)Cache.Set(t, new Cachetable());
            }

            subcache.Reset(mi, clsMethodInfo);

            return((MemberInfo)clsMethodInfo);
        }
Пример #4
0
        // We cache this by mapping MemberInfo to MemberInfo in our
        // cache.  Note that because assemblies can never be unloaded,
        // there's no reason to flush this cache (we won't be holding
        // anything alive longer than it would have been alive anyway.
        public static MemberInfo ConvertToInterfaceMI(MemberInfo mi)
        {
            // First, try to hit the cache:
            MemberInfo cmi = (MemberInfo)Cache.Get(mi);

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

            // Failed to hit the cache, do the lookup.
            // TODO: clean this up a bit.
            // TODO: Deal with non-methodInfo objects (work off MethodBase)
            MethodInfo minfo = mi as MethodInfo;

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

            MethodInfo intfMethodInfo = null;

            // check for AutoDone
            Type reflectType = minfo.ReflectedType;

            if (reflectType.IsInterface)
            {
                intfMethodInfo = minfo;
            }
            else
            {
                // get all the interfaces implemented by the class
                Type[] rgInterfaces = reflectType.GetInterfaces();
                if (rgInterfaces == null)
                {
                    return(null);
                }

                // iterate through all the interfaces
                for (int ii = 0; ii < rgInterfaces.Length; ii++)
                {
                    // get interface mapping for each interface
                    InterfaceMapping imap = reflectType.GetInterfaceMap(rgInterfaces[ii]);

                    if (imap.TargetMethods == null)
                    {
                        continue;
                    }

                    // check if a class method in this inteface map matches
                    for (int j = 0; j < imap.TargetMethods.Length; j++)
                    {
                        // see if the class method matches
                        if (imap.TargetMethods[j] == minfo)
                        {
                            // grab the interface method
                            intfMethodInfo = imap.InterfaceMethods[j];
                            break;
                        }
                    }

                    if (intfMethodInfo != null)
                    {
                        break;
                    }
                }
            }

            DBG.Assert(intfMethodInfo == null ||
                       intfMethodInfo.ReflectedType.IsInterface,
                       "Failed to map class method to interface method");

            Cache.Reset(mi, intfMethodInfo);

            return((MemberInfo)intfMethodInfo);
        }