// Check whether we can cast the transparent proxy to the given type
        public bool CanCastTo(Type castType, Object o)
        {
            bool fCastOK = false;

            // The identity should be non-null
            BCLDebug.Assert(null != IdentityObject, "null != IdentityObject");

            Message.DebugOut("CheckCast for identity " + IdentityObject.GetType());

            if ((castType == s_typeofObject) ||
                (castType == s_typeofMarshalByRefObject))
            {
                return(true);
            }

            // Get the objref of the proxy
            ObjRef oRef = IdentityObject.ObjectRef;

            // If the object ref is non-null then check against the type info
            // stored in the it
            if (null != oRef)
            {
                Object oTP = GetTransparentProxy();

                // Check that there is a matching type in the server object
                // hierarchy represented in the objref
                Message.DebugOut("Calling CanCastTo for type " + castType);
                IRemotingTypeInfo typeInfo = oRef.TypeInfo;
                if (null != typeInfo)
                {
                    fCastOK = typeInfo.CanCastTo(castType, oTP);
                    if (!fCastOK && typeInfo.GetType() == typeof(TypeInfo) && oRef.IsWellKnown())
                    {
                        fCastOK = CanCastToWK(castType);
                    }
                }
                else
                {
                    if (oRef.IsObjRefLite())
                    {
                        // we should do a dynamic cast across the network
                        fCastOK = MarshalByRefObject.CanCastToXmlTypeHelper(castType, (MarshalByRefObject)o);
                    }
                }
            }
            // This is a well known object which does not have a backing ObjRef
            else
            {
                fCastOK = CanCastToWK(castType);
            }
            return(fCastOK);
        }
Exemplo n.º 2
0
        // Check whether we can cast the transparent proxy to the given type
        public bool CanCastTo(Type castType, Object o)
        {
            bool fCastOK = false;

            // The identity should be non-null
            BCLDebug.Assert(null != IdentityObject, "null != IdentityObject");

            Message.DebugOut("CheckCast for identity " + IdentityObject.GetType());

            if ((castType == s_typeofObject) ||
                (castType == s_typeofMarshalByRefObject))
            {
                return(true);
            }

            // Get the objref of the proxy
            ObjRef oRef = IdentityObject.ObjectRef;

            // If the object ref is non-null then check against the type info
            // stored in the it
            if (null != oRef)
            {
                Object oTP = GetTransparentProxy();

                // Check that there is a matching type in the server object
                // hierarchy represented in the objref
                Message.DebugOut("Calling CanCastTo for type " + castType);
                IRemotingTypeInfo typeInfo = oRef.TypeInfo;
                if (null != typeInfo)
                {
                    fCastOK = typeInfo.CanCastTo(castType, oTP);
                }
                else
                {
                    if (oRef.IsObjRefLite())
                    {
                        // we should do a dynamic cast across the network
                        fCastOK = MarshalByRefObject.CanCastToXmlTypeHelper(castType, (MarshalByRefObject)o);
                    }
                }
            }
            // This is a well known object which does not have a backing ObjRef
            else
            {
                Message.DebugOut("CheckCast for well known objects and type " + castType);

                // Check whether the type to which we want to cast is
                // compatible with the current type
                if (castType.IsClass)
                {
                    fCastOK = GetProxiedType().IsAssignableFrom(castType);
                }
                else
                {
                    // NOTE: we are coming here also for x-context proxies
                    // when unmanaged code cannot determine if the cast is not
                    // okay
                    if (!(IdentityObject is ServerIdentity))
                    {
                        BCLDebug.Assert(
                            IdentityObject.URI != null,
                            "Bad WellKnown ID");
                        // Always allow interface casts to succeed. If the
                        // interface is not supported by the well known object
                        // then we will throw an exception when the interface
                        // is invoked.
                        fCastOK = true;
                    }
                }
            }

            return(fCastOK);
        }