Beispiel #1
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);
        }
        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);
        }