示例#1
0
        /**
         * @brief Cast an arbitrary object to the given interface.
         * @param ob = object to be cast of unknown type
         * @returns ob cast to the interface type
         */
        public static Delegate[] CastObj2IFace(object ob, string ifacename)
        {
            if (ob == null)
            {
                return(null);
            }

            /*
             * If it is already one of our interfaces, extract the script-defined class object from it.
             */
            if (ob is Delegate[])
            {
                Delegate[] da = (Delegate[])ob;
                ob = da[0].Target;
            }

            /*
             * Now that we have a presumed script-defined class object, cast that to the requested interface
             * by picking the array of delegates that corresponds to the requested interface.
             */
            XMRSDTypeClObj ci         = (XMRSDTypeClObj)ob;
            int            iFaceIndex = ci.sdtcClass.intfIndices[ifacename];

            return(ci.sdtcITable[iFaceIndex]);
        }
示例#2
0
        /**
         * @brief Perform runtime casting of XMRSDTypeClObj's.
         * @param ob = XMRSDTypeClObj of unknown script-defined class to cast
         * @param classindex = script-defined class to cast it to
         * @returns ob is a valid instance of classindex; else exception thrown
         */
        public static XMRSDTypeClObj CastClass2Class(object ob, int classindex)
        {
            /*
             * Let mono check to see if we at least have an XMRSDTypeClObj.
             */
            XMRSDTypeClObj ci = (XMRSDTypeClObj)ob;

            if (ci != null)
            {
                /*
                 * This is the target class, ie, what we are hoping the object can cast to.
                 */
                TokenDeclSDTypeClass tc = (TokenDeclSDTypeClass)ci.xmrInst.m_ObjCode.sdObjTypesIndx[classindex];

                /*
                 * Step from the object's actual class rootward.
                 * If we find the target class along the way, the cast is valid.
                 * If we run off the end of the root, the cast is not valid.
                 */
                for (TokenDeclSDTypeClass ac = ci.sdtcClass; ac != tc; ac = ac.extends)
                {
                    if (ac == null)
                    {
                        throw new InvalidCastException("invalid cast from " + ci.sdtcClass.longName.val +
                                                       " to " + tc.longName.val);
                    }
                }

                /*
                 * The target class is at or rootward of the actual class,
                 * so the cast is valid.
                 */
            }
            return(ci);
        }