///<summary> /// checks, whether <paramref name="target"/> supports the methods of <paramref name="requiredType"/>. /// Supports testing transparent proxies. ///</summary> ///<param name="target">the target instance or <c>null</c></param> ///<param name="targetName">the name of the target to be used in error messages</param> ///<param name="requiredType">the type to test for</param> /// <exception cref="ArgumentNullException"> /// if <paramref name="requiredType"/> is <c>null</c> /// </exception> /// <exception cref="NotSupportedException"> /// if it is not possible to invoke methods of /// type <paramref name="requiredType"/> on <paramref name="target"/> /// </exception> public static void Understands(object target, string targetName, Type requiredType) { ArgumentNotNull(requiredType, "requiredType"); if (target == null) { throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Target '{0}' is null.", targetName)); } Type targetType; if (RemotingServices.IsTransparentProxy(target)) { RealProxy rp = RemotingServices.GetRealProxy(target); IRemotingTypeInfo rti = rp as IRemotingTypeInfo; if (rti != null) { if (rti.CanCastTo(requiredType, target)) { return; } throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Target '{0}' is a transparent proxy that does not support methods of '{1}'.", targetName, requiredType.FullName)); } targetType = rp.GetProxiedType(); } else { targetType = target.GetType(); } if (!requiredType.IsAssignableFrom(targetType)) { throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Target '{0}' of type '{1}' does not support methods of '{2}'.", targetName, targetType, requiredType.FullName)); } }
// copied from ServicedComponentMarshaler above directly // IF YOU MAK ANY CHANGES TO THIS, KEEP THE OTHER COPY IN SYNC public override void GetObjectData(SerializationInfo info, StreamingContext context) { DBG.Info(DBG.SC, "FSCM: GetObjectData"); ComponentServices.InitializeRemotingChannels(); if (info == null) { throw new ArgumentNullException("info"); } // Check to see if this is a remoting channel, if so, then we // should use the standard objref marshal (cause we're going over SOAP // or another remoting channel). object oClientIsClr = CallContext.GetData("__ClientIsClr"); DBG.Info(DBG.SC, "FSCM: GetObjectData: oClientIsClr = " + oClientIsClr); bool bUseStandardObjRef = (oClientIsClr == null)?false:(bool)oClientIsClr; if (bUseStandardObjRef) { RemoteServicedComponentProxy rscp = _rp as RemoteServicedComponentProxy; if (rscp != null) { DBG.Info(DBG.SC, "FSCM: GetObjectData: intermediary objref"); ObjRef std = RemotingServices.Marshal((MarshalByRefObject)(rscp.RemotingIntermediary.GetTransparentProxy()), null, null); std.GetObjectData(info, context); } else { DBG.Info(DBG.SC, "FSCM: GetObjectData: Using standard objref"); base.GetObjectData(info, context); } } else { DBG.Info(DBG.SC, "FSCM: GetObjectData: Using custom objref"); base.GetObjectData(info, context); //*** base.GetObjectData sets the type to be itself // now wack the type to be us info.SetType(typeof(ServicedComponentMarshaler)); DBG.Assert(_rp != null, "_rp is null"); info.AddValue("servertype", _rp.GetProxiedType()); byte[] dcomBuffer = ComponentServices.GetDCOMBuffer((MarshalByRefObject)_rp.GetTransparentProxy()); DBG.Assert(dcomBuffer != null, "dcomBuffer is null"); if (dcomBuffer != null) { info.AddValue("dcomInfo", dcomBuffer); } } }
/// <summary> /// Determine if the given <see cref="System.Type"/> is assignable from the /// given value, assuming setting by reflection and taking care of transparent proxies. /// </summary> /// <remarks> /// <p> /// Considers primitive wrapper classes as assignable to the /// corresponding primitive types. /// </p> /// <p> /// For example used in an object factory's constructor resolution. /// </p> /// </remarks> /// <param name="type">The target <see cref="System.Type"/>.</param> /// <param name="obj">The value that should be assigned to the type.</param> /// <returns>True if the type is assignable from the value.</returns> public static bool IsAssignable(Type type, object obj) { AssertUtils.ArgumentNotNull(type, "type"); if (!type.IsPrimitive && obj == null) { return(true); } #if !NETSTANDARD if (RemotingServices.IsTransparentProxy(obj)) { RealProxy rp = RemotingServices.GetRealProxy(obj); if (rp is IRemotingTypeInfo remotingTypeInfo) { return(remotingTypeInfo.CanCastTo(type, obj)); } if (rp != null) { type = rp.GetProxiedType(); } if (type == null) { // cannot decide return(false); } } #endif if (type.IsInstanceOfType(obj)) { return(true); } return(type.IsPrimitive && type == typeof(bool) && obj is bool || type == typeof(byte) && obj is byte || type == typeof(char) && obj is char || type == typeof(sbyte) && obj is sbyte || type == typeof(int) && obj is int || type == typeof(short) && obj is short || type == typeof(long) && obj is long || type == typeof(float) && obj is float || type == typeof(double) && obj is double || type == typeof(decimal) && obj is decimal); }
/// <summary> /// Determine if the given <see cref="System.Type"/> is assignable from the /// given value, assuming setting by reflection and taking care of transparent proxies. /// </summary> /// <remarks> /// <p> /// Considers primitive wrapper classes as assignable to the /// corresponding primitive types. /// </p> /// <p> /// For example used in an object factory's constructor resolution. /// </p> /// </remarks> /// <param name="type">The target <see cref="System.Type"/>.</param> /// <param name="obj">The value that should be assigned to the type.</param> /// <returns>True if the type is assignable from the value.</returns> public static bool IsAssignable(Type type, object obj) { AssertUtils.ArgumentNotNull(type, "type"); if (!type.IsPrimitive && obj == null) { return(true); } #if !NETSTANDARD if (RemotingServices.IsTransparentProxy(obj)) { RealProxy rp = RemotingServices.GetRealProxy(obj); if (rp is IRemotingTypeInfo) { return(((IRemotingTypeInfo)rp).CanCastTo(type, obj)); } else if (rp != null) { type = rp.GetProxiedType(); } if (type == null) { // cannot decide return(false); } } #endif return(type.IsInstanceOfType(obj) || (type.Equals(typeof(bool)) && obj is Boolean) || (type.Equals(typeof(byte)) && obj is Byte) || (type.Equals(typeof(char)) && obj is Char) || (type.Equals(typeof(sbyte)) && obj is SByte) || (type.Equals(typeof(int)) && obj is Int32) || (type.Equals(typeof(short)) && obj is Int16) || (type.Equals(typeof(long)) && obj is Int64) || (type.Equals(typeof(float)) && obj is Single) || (type.Equals(typeof(double)) && obj is Double)); }
public TcpClientApp() { const string myHelloServerUrl = "tcp://localhost:8085/TcpHelloServer"; HelloServer obj = (HelloServer)RemotingServices.Connect( typeof(HelloServer), myHelloServerUrl ); //</snippet11> //<snippet18> // GetRealProxy, GetObjectUri, GetEnvoyChainForProxy RealProxy proxy = RemotingServices.GetRealProxy(obj); Console.WriteLine("Real proxy type: {0}", proxy.GetProxiedType().ToString()); Console.WriteLine("Object URI: {0}", RemotingServices.GetObjectUri(obj).ToString()); IMessageSink msgSink = RemotingServices.GetEnvoyChainForProxy(obj).NextSink; //</snippet18> //<snippet12> //IsTransparentProxy, IsObjectOutOfAppDomain, IsObjectOutOfContext Console.WriteLine("Proxy transparent? {0}", RemotingServices.IsTransparentProxy(obj) ? "Yes" : "No" ); Console.WriteLine("Object outside app domain? {0}", RemotingServices.IsObjectOutOfAppDomain(obj) ? "Yes" : "No" ); Console.WriteLine("Object out of context? {0}", RemotingServices.IsObjectOutOfContext(obj) ? "Yes" : "No" ); //</snippet12> //<snippet21> // GetLifetimeService ILease lease = (ILease)RemotingServices.GetLifetimeService(obj); Console.WriteLine("Object lease time remaining: {0}.", lease.CurrentLeaseTime.ToString() ); //</snippet21> //<snippet16> string twoWayMethodArg = "John"; Console.WriteLine("Invoking SayHelloToServerAndWait(\"{0}\").", twoWayMethodArg); Console.WriteLine("Server returned: {0}", obj.SayHelloToServerAndWait(twoWayMethodArg)); //</snippet16> //<snippet17> string oneWayMethodArg = "Mary"; Console.WriteLine("Invoking SayHelloToServer(\"{0}\").", oneWayMethodArg); obj.SayHelloToServer(oneWayMethodArg); //</snippet17> //<snippet23> }
MarshalByRefObject ICustomFactory.CreateInstance(Type serverType) { // Platform.Assert(Platform.W2K, "ServicedComponent"); DBG.Info(DBG.SC, "SCPA.CreateInstance(" + serverType + ") for unmanaged request"); DBG.Assert(ServicedComponentInfo.IsTypeServicedComponent(serverType), "unconfigured type passed to ICustomFactory.CreateInstance"); RealProxy rp = null; // The reason we don't want to cleanup GIT cookies from inside here (passing false) is // because we will already be in a new context, and RevokeInterfaceFromGlobal will be very expensive // (like 5x), as it needs to switch context. A more appropriate place to do this is in the managed CreateInstance // before we even call CoCreateInstance ServicedComponentProxy.CleanupQueues(false); int iSCInfo = ServicedComponentInfo.SCICachedLookup(serverType); bool fIsTypeJITActivated = (iSCInfo & ServicedComponentInfo.SCI_JIT) != 0; bool fIsTypePooled = (iSCInfo & ServicedComponentInfo.SCI_OBJECTPOOLED) != 0; bool fAreMethodsSecure = (iSCInfo & ServicedComponentInfo.SCI_METHODSSECURE) != 0; if (fIsTypeJITActivated) { // NOTE: If the component is JIT activated, we may be trying // to connect a new backing object to an existing TP held // by a managed client. So we look in our handy table // to see if there is a component registered for this context. // Because it is JIT activated, COM+ ensures that it will // have been the distinguished object in this context. IntPtr token = Thunk.Proxy.GetCurrentContextToken(); DBG.Info(DBG.SC, "SCPA.CreateInstance looking for JIT object in IdentityTable. token=" + token); Object otp = IdentityTable.FindObject(token); if (otp != null) { DBG.Info(DBG.SC, "SCPA.CreateInstance found JIT object in IdentityTable."); rp = RemotingServices.GetRealProxy(otp); DBG.Assert(rp is ServicedComponentProxy, "Cached something that wasn't a serviced component proxy in the ID table!"); DBG.Assert(rp != null, " GetTransparentProxy.. real proxy is null"); DBG.Assert(serverType == rp.GetProxiedType(), "Invalid type found in Deactivated list"); } } if (rp == null) { DBG.Info(DBG.SC, "SCPA.CreateInstance creating new SCP fJIT=" + fIsTypeJITActivated + " fPooled=" + fIsTypePooled + " fMethodsSecure=" + fAreMethodsSecure); rp = new ServicedComponentProxy(serverType, fIsTypeJITActivated, fIsTypePooled, fAreMethodsSecure, true); } else if (rp is ServicedComponentProxy) { ServicedComponentProxy scp = (ServicedComponentProxy)rp; scp.ConstructServer(); } MarshalByRefObject mo = (MarshalByRefObject)rp.GetTransparentProxy(); DBG.Assert(mo != null, " GetTransparentProxy returned NULL"); DBG.Info(DBG.SC, "SCPA.ICustomFactory.CreateInstance done."); return(mo); }
internal RemotingIntermediary(RealProxy pxy) : base(pxy.GetProxiedType()) { _pxy = pxy; _blind = new BlindMBRO((MarshalByRefObject)GetTransparentProxy()); }