mscorlib(4.0.0.0) API with additions
RemotingServices.cs
1 using System.Collections;
2 using System.Diagnostics;
4 using System.IO;
5 using System.Reflection;
20 using System.Security;
22 using System.Threading;
23 
25 {
27  [ComVisible(true)]
28  [__DynamicallyInvokable]
29  public static class RemotingServices
30  {
31  private const BindingFlags LookupAll = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
32 
33  private const string FieldGetterName = "FieldGetter";
34 
35  private const string FieldSetterName = "FieldSetter";
36 
37  private const string IsInstanceOfTypeName = "IsInstanceOfType";
38 
39  private const string CanCastToXmlTypeName = "CanCastToXmlType";
40 
41  private const string InvokeMemberName = "InvokeMember";
42 
43  private static volatile MethodBase s_FieldGetterMB;
44 
45  private static volatile MethodBase s_FieldSetterMB;
46 
47  private static volatile MethodBase s_IsInstanceOfTypeMB;
48 
49  private static volatile MethodBase s_CanCastToXmlTypeMB;
50 
51  private static volatile MethodBase s_InvokeMemberMB;
52 
53  private static volatile bool s_bRemoteActivationConfigured;
54 
55  private static volatile bool s_bRegisteredWellKnownChannels;
56 
57  private static bool s_bInProcessOfRegisteringWellKnownChannels;
58 
59  private static readonly object s_delayLoadChannelLock = new object();
60 
64  [MethodImpl(MethodImplOptions.InternalCall)]
65  [SecuritySafeCritical]
66  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
67  public static extern bool IsTransparentProxy(object proxy);
68 
73  [SecuritySafeCritical]
74  public static bool IsObjectOutOfContext(object tp)
75  {
76  if (!IsTransparentProxy(tp))
77  {
78  return false;
79  }
80  RealProxy realProxy = GetRealProxy(tp);
81  Identity identityObject = realProxy.IdentityObject;
82  ServerIdentity serverIdentity = identityObject as ServerIdentity;
83  if (serverIdentity == null || !(realProxy is RemotingProxy))
84  {
85  return true;
86  }
87  return Thread.CurrentContext != serverIdentity.ServerContext;
88  }
89 
94  [__DynamicallyInvokable]
95  public static bool IsObjectOutOfAppDomain(object tp)
96  {
97  return IsClientProxy(tp);
98  }
99 
100  internal static bool IsClientProxy(object obj)
101  {
102  MarshalByRefObject marshalByRefObject = obj as MarshalByRefObject;
103  if (marshalByRefObject == null)
104  {
105  return false;
106  }
107  bool result = false;
108  bool fServer;
109  Identity identity = MarshalByRefObject.GetIdentity(marshalByRefObject, out fServer);
110  if (identity != null && !(identity is ServerIdentity))
111  {
112  result = true;
113  }
114  return result;
115  }
116 
117  [SecurityCritical]
118  internal static bool IsObjectOutOfProcess(object tp)
119  {
120  if (!IsTransparentProxy(tp))
121  {
122  return false;
123  }
124  RealProxy realProxy = GetRealProxy(tp);
125  Identity identityObject = realProxy.IdentityObject;
126  if (identityObject is ServerIdentity)
127  {
128  return false;
129  }
130  if (identityObject != null)
131  {
132  ObjRef objectRef = identityObject.ObjectRef;
133  if (objectRef != null && objectRef.IsFromThisProcess())
134  {
135  return false;
136  }
137  return true;
138  }
139  return true;
140  }
141 
146  [MethodImpl(MethodImplOptions.InternalCall)]
147  [SecurityCritical]
148  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
149  public static extern RealProxy GetRealProxy(object proxy);
150 
151  [MethodImpl(MethodImplOptions.InternalCall)]
152  [SecurityCritical]
153  internal static extern object CreateTransparentProxy(RealProxy rp, RuntimeType typeToProxy, IntPtr stub, object stubData);
154 
155  [SecurityCritical]
156  internal static object CreateTransparentProxy(RealProxy rp, Type typeToProxy, IntPtr stub, object stubData)
157  {
158  RuntimeType runtimeType = typeToProxy as RuntimeType;
159  if (runtimeType == null)
160  {
161  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), "typeToProxy"));
162  }
163  return CreateTransparentProxy(rp, runtimeType, stub, stubData);
164  }
165 
166  [MethodImpl(MethodImplOptions.InternalCall)]
167  [SecurityCritical]
168  internal static extern MarshalByRefObject AllocateUninitializedObject(RuntimeType objectType);
169 
170  [MethodImpl(MethodImplOptions.InternalCall)]
171  [SecurityCritical]
172  internal static extern void CallDefaultCtor(object o);
173 
174  [SecurityCritical]
175  internal static MarshalByRefObject AllocateUninitializedObject(Type objectType)
176  {
177  RuntimeType runtimeType = objectType as RuntimeType;
178  if (runtimeType == null)
179  {
180  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), "objectType"));
181  }
182  return AllocateUninitializedObject(runtimeType);
183  }
184 
185  [MethodImpl(MethodImplOptions.InternalCall)]
186  [SecurityCritical]
187  internal static extern MarshalByRefObject AllocateInitializedObject(RuntimeType objectType);
188 
189  [SecurityCritical]
190  internal static MarshalByRefObject AllocateInitializedObject(Type objectType)
191  {
192  RuntimeType runtimeType = objectType as RuntimeType;
193  if (runtimeType == null)
194  {
195  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), "objectType"));
196  }
197  return AllocateInitializedObject(runtimeType);
198  }
199 
200  [SecurityCritical]
201  internal static bool RegisterWellKnownChannels()
202  {
203  if (!s_bRegisteredWellKnownChannels)
204  {
205  bool lockTaken = false;
206  object configLock = Thread.GetDomain().RemotingData.ConfigLock;
208  try
209  {
210  Monitor.Enter(configLock, ref lockTaken);
211  if (!s_bRegisteredWellKnownChannels && !s_bInProcessOfRegisteringWellKnownChannels)
212  {
213  s_bInProcessOfRegisteringWellKnownChannels = true;
214  CrossAppDomainChannel.RegisterChannel();
215  s_bRegisteredWellKnownChannels = true;
216  }
217  }
218  finally
219  {
220  if (lockTaken)
221  {
222  Monitor.Exit(configLock);
223  }
224  }
225  }
226  return true;
227  }
228 
229  [SecurityCritical]
230  internal static void InternalSetRemoteActivationConfigured()
231  {
232  if (!s_bRemoteActivationConfigured)
233  {
234  nSetRemoteActivationConfigured();
235  s_bRemoteActivationConfigured = true;
236  }
237  }
238 
239  [MethodImpl(MethodImplOptions.InternalCall)]
240  [SecurityCritical]
241  private static extern void nSetRemoteActivationConfigured();
242 
247  [SecurityCritical]
249  {
250  return msg.Uri;
251  }
252 
257  [SecuritySafeCritical]
258  public static object GetLifetimeService(MarshalByRefObject obj)
259  {
260  return obj?.GetLifetimeService();
261  }
262 
267  [SecurityCritical]
268  public static string GetObjectUri(MarshalByRefObject obj)
269  {
270  bool fServer;
271  return MarshalByRefObject.GetIdentity(obj, out fServer)?.URI;
272  }
273 
280  [SecuritySafeCritical]
281  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
282  public static void SetObjectUriForMarshal(MarshalByRefObject obj, string uri)
283  {
284  Identity identity = null;
285  Identity identity2 = null;
286  identity = MarshalByRefObject.GetIdentity(obj, out bool _);
287  identity2 = (identity as ServerIdentity);
288  if (identity != null && identity2 == null)
289  {
290  throw new RemotingException(Environment.GetResourceString("Remoting_SetObjectUriForMarshal__ObjectNeedsToBeLocal"));
291  }
292  if (identity != null && identity.URI != null)
293  {
294  throw new RemotingException(Environment.GetResourceString("Remoting_SetObjectUriForMarshal__UriExists"));
295  }
296  if (identity == null)
297  {
298  Context context = null;
299  context = Thread.GetDomain().GetDefaultContext();
300  ServerIdentity serverIdentity = new ServerIdentity(obj, context, uri);
301  identity = obj.__RaceSetServerIdentity(serverIdentity);
302  if (identity != serverIdentity)
303  {
304  throw new RemotingException(Environment.GetResourceString("Remoting_SetObjectUriForMarshal__UriExists"));
305  }
306  }
307  else
308  {
309  identity.SetOrCreateURI(uri, bIdCtor: true);
310  }
311  }
312 
318  [SecuritySafeCritical]
319  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
320  public static ObjRef Marshal(MarshalByRefObject Obj)
321  {
322  return MarshalInternal(Obj, null, null);
323  }
324 
332  [SecuritySafeCritical]
333  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
334  public static ObjRef Marshal(MarshalByRefObject Obj, string URI)
335  {
336  return MarshalInternal(Obj, URI, null);
337  }
338 
347  [SecuritySafeCritical]
348  [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
349  public static ObjRef Marshal(MarshalByRefObject Obj, string ObjURI, Type RequestedType)
350  {
351  return MarshalInternal(Obj, ObjURI, RequestedType);
352  }
353 
354  [SecurityCritical]
355  internal static ObjRef MarshalInternal(MarshalByRefObject Obj, string ObjURI, Type RequestedType)
356  {
357  return MarshalInternal(Obj, ObjURI, RequestedType, updateChannelData: true);
358  }
359 
360  [SecurityCritical]
361  internal static ObjRef MarshalInternal(MarshalByRefObject Obj, string ObjURI, Type RequestedType, bool updateChannelData)
362  {
363  return MarshalInternal(Obj, ObjURI, RequestedType, updateChannelData, isInitializing: false);
364  }
365 
366  [SecurityCritical]
367  internal static ObjRef MarshalInternal(MarshalByRefObject Obj, string ObjURI, Type RequestedType, bool updateChannelData, bool isInitializing)
368  {
369  if (Obj == null)
370  {
371  return null;
372  }
373  ObjRef objRef = null;
374  Identity identity = null;
375  identity = GetOrCreateIdentity(Obj, ObjURI, isInitializing);
376  if (RequestedType != null)
377  {
378  ServerIdentity serverIdentity = identity as ServerIdentity;
379  if (serverIdentity != null)
380  {
381  serverIdentity.ServerType = RequestedType;
382  serverIdentity.MarshaledAsSpecificType = true;
383  }
384  }
385  objRef = identity.ObjectRef;
386  if (objRef == null)
387  {
388  if (IsTransparentProxy(Obj))
389  {
390  RealProxy realProxy = GetRealProxy(Obj);
391  objRef = realProxy.CreateObjRef(RequestedType);
392  }
393  else
394  {
395  objRef = Obj.CreateObjRef(RequestedType);
396  }
397  if (identity == null || objRef == null)
398  {
399  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidMarshalByRefObject"), "Obj");
400  }
401  objRef = identity.RaceSetObjRef(objRef);
402  }
403  ServerIdentity serverIdentity2 = identity as ServerIdentity;
404  if (serverIdentity2 != null)
405  {
406  MarshalByRefObject obj = null;
407  serverIdentity2.GetServerObjectChain(out obj);
408  Lease lease = identity.Lease;
409  if (lease != null)
410  {
411  lock (lease)
412  {
413  if (lease.CurrentState == LeaseState.Expired)
414  {
415  lease.ActivateLease();
416  }
417  else
418  {
419  lease.RenewInternal(identity.Lease.InitialLeaseTime);
420  }
421  }
422  }
423  if (updateChannelData && objRef.ChannelInfo != null)
424  {
425  object[] currentChannelData = ChannelServices.CurrentChannelData;
426  if (!(Obj is AppDomain))
427  {
428  objRef.ChannelInfo.ChannelData = currentChannelData;
429  }
430  else
431  {
432  int num = currentChannelData.Length;
433  object[] array = new object[num];
434  Array.Copy(currentChannelData, array, num);
435  for (int i = 0; i < num; i++)
436  {
437  if (!(array[i] is CrossAppDomainData))
438  {
439  array[i] = null;
440  }
441  }
442  objRef.ChannelInfo.ChannelData = array;
443  }
444  }
445  }
446  TrackingServices.MarshaledObject(Obj, objRef);
447  return objRef;
448  }
449 
450  [SecurityCritical]
451  private static Identity GetOrCreateIdentity(MarshalByRefObject Obj, string ObjURI, bool isInitializing)
452  {
453  int num = 2;
454  if (isInitializing)
455  {
456  num |= 4;
457  }
458  Identity identity = null;
459  if (IsTransparentProxy(Obj))
460  {
461  RealProxy realProxy = GetRealProxy(Obj);
462  identity = realProxy.IdentityObject;
463  if (identity == null)
464  {
465  identity = IdentityHolder.FindOrCreateServerIdentity(Obj, ObjURI, num);
466  identity.RaceSetTransparentProxy(Obj);
467  }
468  ServerIdentity serverIdentity = identity as ServerIdentity;
469  if (serverIdentity != null)
470  {
471  identity = IdentityHolder.FindOrCreateServerIdentity(serverIdentity.TPOrObject, ObjURI, num);
472  if (ObjURI != null && ObjURI != Identity.RemoveAppNameOrAppGuidIfNecessary(identity.ObjURI))
473  {
474  throw new RemotingException(Environment.GetResourceString("Remoting_URIExists"));
475  }
476  }
477  else if (ObjURI != null && ObjURI != identity.ObjURI)
478  {
479  throw new RemotingException(Environment.GetResourceString("Remoting_URIToProxy"));
480  }
481  }
482  else
483  {
484  identity = IdentityHolder.FindOrCreateServerIdentity(Obj, ObjURI, num);
485  }
486  return identity;
487  }
488 
495  [SecurityCritical]
496  public static void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
497  {
498  if (obj == null)
499  {
500  throw new ArgumentNullException("obj");
501  }
502  if (info == null)
503  {
504  throw new ArgumentNullException("info");
505  }
506  ObjRef objRef = MarshalInternal((MarshalByRefObject)obj, null, null);
507  objRef.GetObjectData(info, context);
508  }
509 
515  [SecurityCritical]
516  public static object Unmarshal(ObjRef objectRef)
517  {
518  return InternalUnmarshal(objectRef, null, fRefine: false);
519  }
520 
528  [SecurityCritical]
529  public static object Unmarshal(ObjRef objectRef, bool fRefine)
530  {
531  return InternalUnmarshal(objectRef, null, fRefine);
532  }
533 
539  [SecurityCritical]
540  [ComVisible(true)]
541  public static object Connect(Type classToProxy, string url)
542  {
543  return Unmarshal(classToProxy, url, null);
544  }
545 
552  [SecurityCritical]
553  [ComVisible(true)]
554  public static object Connect(Type classToProxy, string url, object data)
555  {
556  return Unmarshal(classToProxy, url, data);
557  }
558 
566  [SecurityCritical]
567  public static bool Disconnect(MarshalByRefObject obj)
568  {
569  return Disconnect(obj, bResetURI: true);
570  }
571 
572  [SecurityCritical]
573  internal static bool Disconnect(MarshalByRefObject obj, bool bResetURI)
574  {
575  if (obj == null)
576  {
577  throw new ArgumentNullException("obj");
578  }
579  bool fServer;
580  Identity identity = MarshalByRefObject.GetIdentity(obj, out fServer);
581  bool result = false;
582  if (identity != null)
583  {
584  if (!(identity is ServerIdentity))
585  {
586  throw new RemotingException(Environment.GetResourceString("Remoting_CantDisconnectClientProxy"));
587  }
588  if (identity.IsInIDTable())
589  {
590  IdentityHolder.RemoveIdentity(identity.URI, bResetURI);
591  result = true;
592  }
593  TrackingServices.DisconnectedObject(obj);
594  }
595  return result;
596  }
597 
602  [SecurityCritical]
604  {
605  IMessageSink result = null;
606  if (IsObjectOutOfContext(obj))
607  {
608  RealProxy realProxy = GetRealProxy(obj);
609  Identity identityObject = realProxy.IdentityObject;
610  if (identityObject != null)
611  {
612  result = identityObject.EnvoyChain;
613  }
614  }
615  return result;
616  }
617 
622  [SecurityCritical]
624  {
625  ObjRef result = null;
626  if (!IsTransparentProxy(obj))
627  {
628  throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_BadType"));
629  }
630  RealProxy realProxy = GetRealProxy(obj);
631  Identity identityObject = realProxy.IdentityObject;
632  if (identityObject != null)
633  {
634  result = identityObject.ObjectRef;
635  }
636  return result;
637  }
638 
639  [SecurityCritical]
640  internal static object Unmarshal(Type classToProxy, string url)
641  {
642  return Unmarshal(classToProxy, url, null);
643  }
644 
645  [SecurityCritical]
646  internal static object Unmarshal(Type classToProxy, string url, object data)
647  {
648  if (null == classToProxy)
649  {
650  throw new ArgumentNullException("classToProxy");
651  }
652  if (url == null)
653  {
654  throw new ArgumentNullException("url");
655  }
656  if (!classToProxy.IsMarshalByRef && !classToProxy.IsInterface)
657  {
658  throw new RemotingException(Environment.GetResourceString("Remoting_NotRemotableByReference"));
659  }
660  Identity identity = IdentityHolder.ResolveIdentity(url);
661  object obj = null;
662  if (identity == null || identity.ChannelSink == null || identity.EnvoyChain == null)
663  {
664  string text = null;
665  IMessageSink chnlSink = null;
666  IMessageSink envoySink = null;
667  text = CreateEnvoyAndChannelSinks(url, data, out chnlSink, out envoySink);
668  if (chnlSink == null)
669  {
670  throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Connect_CantCreateChannelSink"), url));
671  }
672  if (text == null)
673  {
674  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidUrl"));
675  }
676  identity = IdentityHolder.FindOrCreateIdentity(text, url, null);
677  SetEnvoyAndChannelSinks(identity, chnlSink, envoySink);
678  }
679  return GetOrCreateProxy(classToProxy, identity);
680  }
681 
682  [SecurityCritical]
683  internal static object Wrap(ContextBoundObject obj)
684  {
685  return Wrap(obj, null, fCreateSinks: true);
686  }
687 
688  [SecurityCritical]
689  internal static object Wrap(ContextBoundObject obj, object proxy, bool fCreateSinks)
690  {
691  if (obj != null && !IsTransparentProxy(obj))
692  {
693  Identity identity = null;
694  if (proxy != null)
695  {
696  RealProxy realProxy = GetRealProxy(proxy);
697  if (realProxy.UnwrappedServerObject == null)
698  {
699  realProxy.AttachServerHelper(obj);
700  }
701  identity = MarshalByRefObject.GetIdentity(obj);
702  }
703  else
704  {
705  identity = IdentityHolder.FindOrCreateServerIdentity(obj, null, 0);
706  }
707  proxy = GetOrCreateProxy(identity, proxy, fRefine: true);
708  GetRealProxy(proxy).Wrap();
709  if (fCreateSinks)
710  {
711  IMessageSink chnlSink = null;
712  IMessageSink envoySink = null;
713  CreateEnvoyAndChannelSinks((MarshalByRefObject)proxy, null, out chnlSink, out envoySink);
714  SetEnvoyAndChannelSinks(identity, chnlSink, envoySink);
715  }
716  RealProxy realProxy2 = GetRealProxy(proxy);
717  if (realProxy2.UnwrappedServerObject == null)
718  {
719  realProxy2.AttachServerHelper(obj);
720  }
721  return proxy;
722  }
723  return obj;
724  }
725 
726  internal static string GetObjectUriFromFullUri(string fullUri)
727  {
728  if (fullUri == null)
729  {
730  return null;
731  }
732  int num = fullUri.LastIndexOf('/');
733  if (num == -1)
734  {
735  return fullUri;
736  }
737  return fullUri.Substring(num + 1);
738  }
739 
740  [MethodImpl(MethodImplOptions.InternalCall)]
741  [SecurityCritical]
742  internal static extern object Unwrap(ContextBoundObject obj);
743 
744  [MethodImpl(MethodImplOptions.InternalCall)]
745  [SecurityCritical]
746  internal static extern object AlwaysUnwrap(ContextBoundObject obj);
747 
748  [SecurityCritical]
749  internal static object InternalUnmarshal(ObjRef objectRef, object proxy, bool fRefine)
750  {
751  object obj = null;
752  Identity identity = null;
753  Context currentContext = Thread.CurrentContext;
754  if (!ObjRef.IsWellFormed(objectRef))
755  {
756  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_BadObjRef"), "Unmarshal"));
757  }
758  if (objectRef.IsWellKnown())
759  {
760  obj = Unmarshal(typeof(MarshalByRefObject), objectRef.URI);
761  identity = IdentityHolder.ResolveIdentity(objectRef.URI);
762  if (identity.ObjectRef == null)
763  {
764  identity.RaceSetObjRef(objectRef);
765  }
766  return obj;
767  }
768  identity = IdentityHolder.FindOrCreateIdentity(objectRef.URI, null, objectRef);
769  currentContext = Thread.CurrentContext;
770  ServerIdentity serverIdentity = identity as ServerIdentity;
771  if (serverIdentity != null)
772  {
773  currentContext = Thread.CurrentContext;
774  if (!serverIdentity.IsContextBound)
775  {
776  if (proxy != null)
777  {
778  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadInternalState_ProxySameAppDomain")));
779  }
780  obj = serverIdentity.TPOrObject;
781  }
782  else
783  {
784  IMessageSink chnlSink = null;
785  IMessageSink envoySink = null;
786  CreateEnvoyAndChannelSinks(serverIdentity.TPOrObject, null, out chnlSink, out envoySink);
787  SetEnvoyAndChannelSinks(identity, chnlSink, envoySink);
788  obj = GetOrCreateProxy(identity, proxy, fRefine: true);
789  }
790  }
791  else
792  {
793  IMessageSink chnlSink2 = null;
794  IMessageSink envoySink2 = null;
795  if (!objectRef.IsObjRefLite())
796  {
797  CreateEnvoyAndChannelSinks(null, objectRef, out chnlSink2, out envoySink2);
798  }
799  else
800  {
801  CreateEnvoyAndChannelSinks(objectRef.URI, null, out chnlSink2, out envoySink2);
802  }
803  SetEnvoyAndChannelSinks(identity, chnlSink2, envoySink2);
804  if (objectRef.HasProxyAttribute())
805  {
806  fRefine = true;
807  }
808  obj = GetOrCreateProxy(identity, proxy, fRefine);
809  }
810  TrackingServices.UnmarshaledObject(obj, objectRef);
811  return obj;
812  }
813 
814  [SecurityCritical]
815  private static object GetOrCreateProxy(Identity idObj, object proxy, bool fRefine)
816  {
817  if (proxy == null)
818  {
819  ServerIdentity serverIdentity = idObj as ServerIdentity;
820  Type type;
821  if (serverIdentity != null)
822  {
823  type = serverIdentity.ServerType;
824  }
825  else
826  {
827  IRemotingTypeInfo typeInfo = idObj.ObjectRef.TypeInfo;
828  type = null;
829  if ((typeInfo is TypeInfo && !fRefine) || typeInfo == null)
830  {
831  type = typeof(MarshalByRefObject);
832  }
833  else
834  {
835  string typeName = typeInfo.TypeName;
836  if (typeName != null)
837  {
838  string typeName2 = null;
839  string assemName = null;
840  TypeInfo.ParseTypeAndAssembly(typeName, out typeName2, out assemName);
841  Assembly assembly = FormatterServices.LoadAssemblyFromStringNoThrow(assemName);
842  if (assembly != null)
843  {
844  type = assembly.GetType(typeName2, throwOnError: false, ignoreCase: false);
845  }
846  }
847  }
848  if (null == type)
849  {
850  throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), typeInfo.TypeName));
851  }
852  }
853  proxy = SetOrCreateProxy(idObj, type, null);
854  }
855  else
856  {
857  proxy = SetOrCreateProxy(idObj, null, proxy);
858  }
859  if (proxy == null)
860  {
861  throw new RemotingException(Environment.GetResourceString("Remoting_UnexpectedNullTP"));
862  }
863  return proxy;
864  }
865 
866  [SecurityCritical]
867  private static object GetOrCreateProxy(Type classToProxy, Identity idObj)
868  {
869  object obj = idObj.TPOrObject;
870  if (obj == null)
871  {
872  obj = SetOrCreateProxy(idObj, classToProxy, null);
873  }
874  ServerIdentity serverIdentity = idObj as ServerIdentity;
875  if (serverIdentity != null)
876  {
877  Type serverType = serverIdentity.ServerType;
878  if (!classToProxy.IsAssignableFrom(serverType))
879  {
880  throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), serverType.FullName, classToProxy.FullName));
881  }
882  }
883  return obj;
884  }
885 
886  [SecurityCritical]
887  private static MarshalByRefObject SetOrCreateProxy(Identity idObj, Type classToProxy, object proxy)
888  {
889  RealProxy realProxy = null;
890  if (proxy == null)
891  {
892  ServerIdentity serverIdentity = idObj as ServerIdentity;
893  if (idObj.ObjectRef != null)
894  {
895  ProxyAttribute proxyAttribute = ActivationServices.GetProxyAttribute(classToProxy);
896  realProxy = proxyAttribute.CreateProxy(idObj.ObjectRef, classToProxy, null, null);
897  }
898  if (realProxy == null)
899  {
900  ProxyAttribute defaultProxyAttribute = ActivationServices.DefaultProxyAttribute;
901  realProxy = defaultProxyAttribute.CreateProxy(idObj.ObjectRef, classToProxy, null, serverIdentity?.ServerContext);
902  }
903  }
904  else
905  {
906  realProxy = GetRealProxy(proxy);
907  }
908  realProxy.IdentityObject = idObj;
909  proxy = realProxy.GetTransparentProxy();
910  proxy = idObj.RaceSetTransparentProxy(proxy);
911  return (MarshalByRefObject)proxy;
912  }
913 
914  private static bool AreChannelDataElementsNull(object[] channelData)
915  {
916  foreach (object obj in channelData)
917  {
918  if (obj != null)
919  {
920  return false;
921  }
922  }
923  return true;
924  }
925 
926  [SecurityCritical]
927  internal static void CreateEnvoyAndChannelSinks(MarshalByRefObject tpOrObject, ObjRef objectRef, out IMessageSink chnlSink, out IMessageSink envoySink)
928  {
929  chnlSink = null;
930  envoySink = null;
931  if (objectRef == null)
932  {
933  chnlSink = ChannelServices.GetCrossContextChannelSink();
934  envoySink = Thread.CurrentContext.CreateEnvoyChain(tpOrObject);
935  return;
936  }
937  object[] channelData = objectRef.ChannelInfo.ChannelData;
938  if (channelData != null && !AreChannelDataElementsNull(channelData))
939  {
940  for (int i = 0; i < channelData.Length; i++)
941  {
942  chnlSink = ChannelServices.CreateMessageSink(channelData[i]);
943  if (chnlSink != null)
944  {
945  break;
946  }
947  }
948  if (chnlSink == null)
949  {
950  lock (s_delayLoadChannelLock)
951  {
952  for (int j = 0; j < channelData.Length; j++)
953  {
954  chnlSink = ChannelServices.CreateMessageSink(channelData[j]);
955  if (chnlSink != null)
956  {
957  break;
958  }
959  }
960  if (chnlSink == null)
961  {
962  object[] array = channelData;
963  foreach (object data in array)
964  {
965  chnlSink = RemotingConfigHandler.FindDelayLoadChannelForCreateMessageSink(null, data, out string _);
966  if (chnlSink != null)
967  {
968  break;
969  }
970  }
971  }
972  }
973  }
974  }
975  if (objectRef.EnvoyInfo != null && objectRef.EnvoyInfo.EnvoySinks != null)
976  {
977  envoySink = objectRef.EnvoyInfo.EnvoySinks;
978  }
979  else
980  {
981  envoySink = EnvoyTerminatorSink.MessageSink;
982  }
983  }
984 
985  [SecurityCritical]
986  internal static string CreateEnvoyAndChannelSinks(string url, object data, out IMessageSink chnlSink, out IMessageSink envoySink)
987  {
988  string text = null;
989  text = CreateChannelSink(url, data, out chnlSink);
990  envoySink = EnvoyTerminatorSink.MessageSink;
991  return text;
992  }
993 
994  [SecurityCritical]
995  private static string CreateChannelSink(string url, object data, out IMessageSink chnlSink)
996  {
997  string objectURI = null;
998  chnlSink = ChannelServices.CreateMessageSink(url, data, out objectURI);
999  if (chnlSink == null)
1000  {
1001  lock (s_delayLoadChannelLock)
1002  {
1003  chnlSink = ChannelServices.CreateMessageSink(url, data, out objectURI);
1004  if (chnlSink != null)
1005  {
1006  return objectURI;
1007  }
1008  chnlSink = RemotingConfigHandler.FindDelayLoadChannelForCreateMessageSink(url, data, out objectURI);
1009  return objectURI;
1010  }
1011  }
1012  return objectURI;
1013  }
1014 
1015  internal static void SetEnvoyAndChannelSinks(Identity idObj, IMessageSink chnlSink, IMessageSink envoySink)
1016  {
1017  if (idObj.ChannelSink == null && chnlSink != null)
1018  {
1019  idObj.RaceSetChannelSink(chnlSink);
1020  }
1021  if (idObj.EnvoyChain == null)
1022  {
1023  if (envoySink == null)
1024  {
1025  throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadInternalState_FailEnvoySink")));
1026  }
1027  idObj.RaceSetEnvoyChain(envoySink);
1028  }
1029  }
1030 
1031  [SecurityCritical]
1032  private static bool CheckCast(RealProxy rp, RuntimeType castType)
1033  {
1034  bool result = false;
1035  if (castType == typeof(object))
1036  {
1037  return true;
1038  }
1039  if (!castType.IsInterface && !castType.IsMarshalByRef)
1040  {
1041  return false;
1042  }
1043  if (castType != typeof(IObjectReference))
1044  {
1045  IRemotingTypeInfo remotingTypeInfo = rp as IRemotingTypeInfo;
1046  if (remotingTypeInfo != null)
1047  {
1048  result = remotingTypeInfo.CanCastTo(castType, rp.GetTransparentProxy());
1049  }
1050  else
1051  {
1052  Identity identityObject = rp.IdentityObject;
1053  if (identityObject != null)
1054  {
1055  ObjRef objectRef = identityObject.ObjectRef;
1056  if (objectRef != null)
1057  {
1058  remotingTypeInfo = objectRef.TypeInfo;
1059  if (remotingTypeInfo != null)
1060  {
1061  result = remotingTypeInfo.CanCastTo(castType, rp.GetTransparentProxy());
1062  }
1063  }
1064  }
1065  }
1066  }
1067  return result;
1068  }
1069 
1070  [SecurityCritical]
1071  internal static bool ProxyCheckCast(RealProxy rp, RuntimeType castType)
1072  {
1073  return CheckCast(rp, castType);
1074  }
1075 
1076  [MethodImpl(MethodImplOptions.InternalCall)]
1077  [SecurityCritical]
1078  internal static extern object CheckCast(object objToExpand, RuntimeType type);
1079 
1080  [SecurityCritical]
1081  internal static GCHandle CreateDelegateInvocation(WaitCallback waitDelegate, object state)
1082  {
1083  return GCHandle.Alloc(new object[2]
1084  {
1085  waitDelegate,
1086  state
1087  });
1088  }
1089 
1090  [SecurityCritical]
1091  internal static void DisposeDelegateInvocation(GCHandle delegateCallToken)
1092  {
1093  delegateCallToken.Free();
1094  }
1095 
1096  [SecurityCritical]
1097  internal static object CreateProxyForDomain(int appDomainId, IntPtr defCtxID)
1098  {
1099  AppDomain appDomain = null;
1100  ObjRef objectRef = CreateDataForDomain(appDomainId, defCtxID);
1101  return (AppDomain)Unmarshal(objectRef);
1102  }
1103 
1104  [SecurityCritical]
1105  internal static object CreateDataForDomainCallback(object[] args)
1106  {
1107  RegisterWellKnownChannels();
1108  ObjRef objRef = MarshalInternal(Thread.CurrentContext.AppDomain, null, null, updateChannelData: false);
1109  ServerIdentity serverIdentity = (ServerIdentity)MarshalByRefObject.GetIdentity(Thread.CurrentContext.AppDomain);
1110  serverIdentity.SetHandle();
1111  objRef.SetServerIdentity(serverIdentity.GetHandle());
1112  objRef.SetDomainID(AppDomain.CurrentDomain.GetId());
1113  return objRef;
1114  }
1115 
1116  [SecurityCritical]
1117  internal static ObjRef CreateDataForDomain(int appDomainId, IntPtr defCtxID)
1118  {
1119  RegisterWellKnownChannels();
1120  InternalCrossContextDelegate ftnToCall = CreateDataForDomainCallback;
1121  return (ObjRef)Thread.CurrentThread.InternalCrossContextCallback(null, defCtxID, appDomainId, ftnToCall, null);
1122  }
1123 
1128  [SecurityCritical]
1130  {
1131  return InternalGetMethodBaseFromMethodMessage(msg);
1132  }
1133 
1134  [SecurityCritical]
1135  internal static MethodBase InternalGetMethodBaseFromMethodMessage(IMethodMessage msg)
1136  {
1137  if (msg == null)
1138  {
1139  return null;
1140  }
1141  Type type = InternalGetTypeFromQualifiedTypeName(msg.TypeName);
1142  if (type == null)
1143  {
1144  throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), msg.TypeName));
1145  }
1146  Type[] signature = (Type[])msg.MethodSignature;
1147  return GetMethodBase(msg, type, signature);
1148  }
1149 
1155  [SecurityCritical]
1156  public static bool IsMethodOverloaded(IMethodMessage msg)
1157  {
1158  RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(msg.MethodBase);
1159  return reflectionCachedData.IsOverloaded();
1160  }
1161 
1162  [SecurityCritical]
1163  private static MethodBase GetMethodBase(IMethodMessage msg, Type t, Type[] signature)
1164  {
1165  MethodBase result = null;
1167  {
1168  if (signature == null)
1169  {
1170  RuntimeType runtimeType = t as RuntimeType;
1171  ConstructorInfo[] array = (!(runtimeType == null)) ? runtimeType.GetConstructors() : t.GetConstructors();
1172  if (1 != array.Length)
1173  {
1174  throw new AmbiguousMatchException(Environment.GetResourceString("Remoting_AmbiguousCTOR"));
1175  }
1176  result = array[0];
1177  }
1178  else
1179  {
1180  RuntimeType runtimeType2 = t as RuntimeType;
1181  result = ((!(runtimeType2 == null)) ? runtimeType2.GetConstructor(signature) : t.GetConstructor(signature));
1182  }
1183  }
1184  else if (msg is IMethodCallMessage || msg is IMethodReturnMessage)
1185  {
1186  if (signature == null)
1187  {
1188  RuntimeType runtimeType3 = t as RuntimeType;
1189  result = ((!(runtimeType3 == null)) ? runtimeType3.GetMethod(msg.MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) : t.GetMethod(msg.MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
1190  }
1191  else
1192  {
1193  RuntimeType runtimeType4 = t as RuntimeType;
1194  result = ((!(runtimeType4 == null)) ? runtimeType4.GetMethod(msg.MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, signature, null) : t.GetMethod(msg.MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, signature, null));
1195  }
1196  }
1197  return result;
1198  }
1199 
1200  [SecurityCritical]
1201  internal static bool IsMethodAllowedRemotely(MethodBase method)
1202  {
1203  if (s_FieldGetterMB == null || s_FieldSetterMB == null || s_IsInstanceOfTypeMB == null || s_InvokeMemberMB == null || s_CanCastToXmlTypeMB == null)
1204  {
1205  CodeAccessPermission.Assert(allPossible: true);
1206  if (s_FieldGetterMB == null)
1207  {
1208  s_FieldGetterMB = typeof(object).GetMethod("FieldGetter", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1209  }
1210  if (s_FieldSetterMB == null)
1211  {
1212  s_FieldSetterMB = typeof(object).GetMethod("FieldSetter", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1213  }
1214  if (s_IsInstanceOfTypeMB == null)
1215  {
1216  s_IsInstanceOfTypeMB = typeof(MarshalByRefObject).GetMethod("IsInstanceOfType", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1217  }
1218  if (s_CanCastToXmlTypeMB == null)
1219  {
1220  s_CanCastToXmlTypeMB = typeof(MarshalByRefObject).GetMethod("CanCastToXmlType", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1221  }
1222  if (s_InvokeMemberMB == null)
1223  {
1224  s_InvokeMemberMB = typeof(MarshalByRefObject).GetMethod("InvokeMember", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1225  }
1226  }
1227  if (!(method == s_FieldGetterMB) && !(method == s_FieldSetterMB) && !(method == s_IsInstanceOfTypeMB) && !(method == s_InvokeMemberMB))
1228  {
1229  return method == s_CanCastToXmlTypeMB;
1230  }
1231  return true;
1232  }
1233 
1239  [SecurityCritical]
1240  public static bool IsOneWay(MethodBase method)
1241  {
1242  if (method == null)
1243  {
1244  return false;
1245  }
1246  RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(method);
1247  return reflectionCachedData.IsOneWayMethod();
1248  }
1249 
1250  internal static bool FindAsyncMethodVersion(MethodInfo method, out MethodInfo beginMethod, out MethodInfo endMethod)
1251  {
1252  beginMethod = null;
1253  endMethod = null;
1254  string value = "Begin" + method.Name;
1255  string value2 = "End" + method.Name;
1256  ArrayList arrayList = new ArrayList();
1257  ArrayList arrayList2 = new ArrayList();
1258  Type typeFromHandle = typeof(IAsyncResult);
1259  Type returnType = method.ReturnType;
1260  ParameterInfo[] parameters = method.GetParameters();
1261  ParameterInfo[] array = parameters;
1262  foreach (ParameterInfo parameterInfo in array)
1263  {
1264  if (parameterInfo.IsOut)
1265  {
1266  arrayList2.Add(parameterInfo);
1267  }
1268  else if (parameterInfo.ParameterType.IsByRef)
1269  {
1270  arrayList.Add(parameterInfo);
1271  arrayList2.Add(parameterInfo);
1272  }
1273  else
1274  {
1275  arrayList.Add(parameterInfo);
1276  }
1277  }
1278  arrayList.Add(typeof(AsyncCallback));
1279  arrayList.Add(typeof(object));
1280  arrayList2.Add(typeof(IAsyncResult));
1281  Type declaringType = method.DeclaringType;
1282  MethodInfo[] methods = declaringType.GetMethods();
1283  MethodInfo[] array2 = methods;
1284  foreach (MethodInfo methodInfo in array2)
1285  {
1286  ParameterInfo[] parameters2 = methodInfo.GetParameters();
1287  if (methodInfo.Name.Equals(value) && methodInfo.ReturnType == typeFromHandle && CompareParameterList(arrayList, parameters2))
1288  {
1289  beginMethod = methodInfo;
1290  }
1291  else if (methodInfo.Name.Equals(value2) && methodInfo.ReturnType == returnType && CompareParameterList(arrayList2, parameters2))
1292  {
1293  endMethod = methodInfo;
1294  }
1295  }
1296  if (beginMethod != null && endMethod != null)
1297  {
1298  return true;
1299  }
1300  return false;
1301  }
1302 
1303  private static bool CompareParameterList(ArrayList params1, ParameterInfo[] params2)
1304  {
1305  if (params1.Count != params2.Length)
1306  {
1307  return false;
1308  }
1309  int num = 0;
1310  foreach (object item in params1)
1311  {
1312  ParameterInfo parameterInfo = params2[num];
1313  ParameterInfo parameterInfo2 = item as ParameterInfo;
1314  if (parameterInfo2 != null)
1315  {
1316  if (parameterInfo2.ParameterType != parameterInfo.ParameterType || parameterInfo2.IsIn != parameterInfo.IsIn || parameterInfo2.IsOut != parameterInfo.IsOut)
1317  {
1318  return false;
1319  }
1320  }
1321  else if ((Type)item != parameterInfo.ParameterType && parameterInfo.IsIn)
1322  {
1323  return false;
1324  }
1325  num++;
1326  }
1327  return true;
1328  }
1329 
1334  [SecurityCritical]
1335  public static Type GetServerTypeForUri(string URI)
1336  {
1337  Type result = null;
1338  if (URI != null)
1339  {
1340  ServerIdentity serverIdentity = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
1341  result = ((serverIdentity != null) ? serverIdentity.ServerType : RemotingConfigHandler.GetServerTypeForUri(URI));
1342  }
1343  return result;
1344  }
1345 
1346  [SecurityCritical]
1347  internal static void DomainUnloaded(int domainID)
1348  {
1349  IdentityHolder.FlushIdentityTable();
1350  CrossAppDomainSink.DomainUnloaded(domainID);
1351  }
1352 
1353  [SecurityCritical]
1354  internal static IntPtr GetServerContextForProxy(object tp)
1355  {
1356  ObjRef objRef = null;
1357  bool bSameDomain;
1358  int domainId;
1359  return GetServerContextForProxy(tp, out objRef, out bSameDomain, out domainId);
1360  }
1361 
1362  [SecurityCritical]
1363  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1364  internal static int GetServerDomainIdForProxy(object tp)
1365  {
1366  RealProxy realProxy = GetRealProxy(tp);
1367  Identity identityObject = realProxy.IdentityObject;
1368  return identityObject.ObjectRef.GetServerDomainId();
1369  }
1370 
1371  [SecurityCritical]
1372  internal static void GetServerContextAndDomainIdForProxy(object tp, out IntPtr contextId, out int domainId)
1373  {
1374  contextId = GetServerContextForProxy(tp, out ObjRef _, out bool _, out domainId);
1375  }
1376 
1377  [SecurityCritical]
1378  private static IntPtr GetServerContextForProxy(object tp, out ObjRef objRef, out bool bSameDomain, out int domainId)
1379  {
1380  IntPtr result = IntPtr.Zero;
1381  objRef = null;
1382  bSameDomain = false;
1383  domainId = 0;
1384  if (IsTransparentProxy(tp))
1385  {
1386  RealProxy realProxy = GetRealProxy(tp);
1387  Identity identityObject = realProxy.IdentityObject;
1388  if (identityObject != null)
1389  {
1390  ServerIdentity serverIdentity = identityObject as ServerIdentity;
1391  if (serverIdentity != null)
1392  {
1393  bSameDomain = true;
1394  result = serverIdentity.ServerContext.InternalContextID;
1395  domainId = Thread.GetDomain().GetId();
1396  }
1397  else
1398  {
1399  objRef = identityObject.ObjectRef;
1400  result = ((objRef == null) ? IntPtr.Zero : objRef.GetServerContext(out domainId));
1401  }
1402  }
1403  else
1404  {
1405  result = Context.DefaultContext.InternalContextID;
1406  }
1407  }
1408  return result;
1409  }
1410 
1411  [SecurityCritical]
1412  internal static Context GetServerContext(MarshalByRefObject obj)
1413  {
1414  Context result = null;
1415  if (!IsTransparentProxy(obj) && obj is ContextBoundObject)
1416  {
1417  result = Thread.CurrentContext;
1418  }
1419  else
1420  {
1421  RealProxy realProxy = GetRealProxy(obj);
1422  Identity identityObject = realProxy.IdentityObject;
1423  ServerIdentity serverIdentity = identityObject as ServerIdentity;
1424  if (serverIdentity != null)
1425  {
1426  result = serverIdentity.ServerContext;
1427  }
1428  }
1429  return result;
1430  }
1431 
1432  [SecurityCritical]
1433  private static object GetType(object tp)
1434  {
1435  Type result = null;
1436  RealProxy realProxy = GetRealProxy(tp);
1437  Identity identityObject = realProxy.IdentityObject;
1438  if (identityObject != null && identityObject.ObjectRef != null && identityObject.ObjectRef.TypeInfo != null)
1439  {
1440  IRemotingTypeInfo typeInfo = identityObject.ObjectRef.TypeInfo;
1441  string typeName = typeInfo.TypeName;
1442  if (typeName != null)
1443  {
1444  result = InternalGetTypeFromQualifiedTypeName(typeName);
1445  }
1446  }
1447  return result;
1448  }
1449 
1450  [SecurityCritical]
1451  internal static byte[] MarshalToBuffer(object o, bool crossRuntime)
1452  {
1453  if (crossRuntime)
1454  {
1455  if (IsTransparentProxy(o))
1456  {
1457  if (GetRealProxy(o) is RemotingProxy && ChannelServices.RegisteredChannels.Length == 0)
1458  {
1459  return null;
1460  }
1461  }
1462  else
1463  {
1464  MarshalByRefObject marshalByRefObject = o as MarshalByRefObject;
1465  if (marshalByRefObject != null)
1466  {
1467  ProxyAttribute proxyAttribute = ActivationServices.GetProxyAttribute(marshalByRefObject.GetType());
1468  if (proxyAttribute == ActivationServices.DefaultProxyAttribute && ChannelServices.RegisteredChannels.Length == 0)
1469  {
1470  return null;
1471  }
1472  }
1473  }
1474  }
1475  MemoryStream memoryStream = new MemoryStream();
1476  RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector();
1477  BinaryFormatter binaryFormatter = new BinaryFormatter();
1478  binaryFormatter.SurrogateSelector = surrogateSelector;
1479  binaryFormatter.Context = new StreamingContext(StreamingContextStates.Other);
1480  binaryFormatter.Serialize(memoryStream, o, null, fCheck: false);
1481  return memoryStream.GetBuffer();
1482  }
1483 
1484  [SecurityCritical]
1485  internal static object UnmarshalFromBuffer(byte[] b, bool crossRuntime)
1486  {
1487  MemoryStream serializationStream = new MemoryStream(b);
1488  BinaryFormatter binaryFormatter = new BinaryFormatter();
1489  binaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
1490  binaryFormatter.SurrogateSelector = null;
1491  binaryFormatter.Context = new StreamingContext(StreamingContextStates.Other);
1492  object obj = binaryFormatter.Deserialize(serializationStream, null, fCheck: false);
1493  if (crossRuntime && IsTransparentProxy(obj))
1494  {
1495  if (!(GetRealProxy(obj) is RemotingProxy))
1496  {
1497  return obj;
1498  }
1499  if (ChannelServices.RegisteredChannels.Length == 0)
1500  {
1501  return null;
1502  }
1503  obj.GetHashCode();
1504  }
1505  return obj;
1506  }
1507 
1508  internal static object UnmarshalReturnMessageFromBuffer(byte[] b, IMethodCallMessage msg)
1509  {
1510  MemoryStream serializationStream = new MemoryStream(b);
1511  BinaryFormatter binaryFormatter = new BinaryFormatter();
1512  binaryFormatter.SurrogateSelector = null;
1513  binaryFormatter.Context = new StreamingContext(StreamingContextStates.Other);
1514  return binaryFormatter.DeserializeMethodResponse(serializationStream, null, msg);
1515  }
1516 
1523  [SecurityCritical]
1525  {
1526  if (target == null)
1527  {
1528  throw new ArgumentNullException("target");
1529  }
1530  RealProxy realProxy = GetRealProxy(target);
1531  if (realProxy is RemotingProxy && !realProxy.DoContextsMatch())
1532  {
1533  throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_WrongContext"));
1534  }
1535  StackBuilderSink stackBuilderSink = new StackBuilderSink(target);
1536  return (IMethodReturnMessage)stackBuilderSink.SyncProcessMessage(reqMsg);
1537  }
1538 
1539  [SecurityCritical]
1540  internal static string DetermineDefaultQualifiedTypeName(Type type)
1541  {
1542  if (type == null)
1543  {
1544  throw new ArgumentNullException("type");
1545  }
1546  string xmlType = null;
1547  string xmlTypeNamespace = null;
1548  if (SoapServices.GetXmlTypeForInteropType(type, out xmlType, out xmlTypeNamespace))
1549  {
1550  return "soap:" + xmlType + ", " + xmlTypeNamespace;
1551  }
1552  return type.AssemblyQualifiedName;
1553  }
1554 
1555  [SecurityCritical]
1556  internal static string GetDefaultQualifiedTypeName(RuntimeType type)
1557  {
1558  RemotingTypeCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(type);
1559  return reflectionCachedData.QualifiedTypeName;
1560  }
1561 
1562  internal static string InternalGetClrTypeNameFromQualifiedTypeName(string qualifiedTypeName)
1563  {
1564  if (qualifiedTypeName.Length > 4 && string.CompareOrdinal(qualifiedTypeName, 0, "clr:", 0, 4) == 0)
1565  {
1566  return qualifiedTypeName.Substring(4);
1567  }
1568  return null;
1569  }
1570 
1571  private static int IsSoapType(string qualifiedTypeName)
1572  {
1573  if (qualifiedTypeName.Length > 5 && string.CompareOrdinal(qualifiedTypeName, 0, "soap:", 0, 5) == 0)
1574  {
1575  return qualifiedTypeName.IndexOf(',', 5);
1576  }
1577  return -1;
1578  }
1579 
1580  [SecurityCritical]
1581  internal static string InternalGetSoapTypeNameFromQualifiedTypeName(string xmlTypeName, string xmlTypeNamespace)
1582  {
1583  if (!SoapServices.DecodeXmlNamespaceForClrTypeNamespace(xmlTypeNamespace, out string typeNamespace, out string assemblyName))
1584  {
1585  return null;
1586  }
1587  string str = (typeNamespace == null || typeNamespace.Length <= 0) ? xmlTypeName : (typeNamespace + "." + xmlTypeName);
1588  try
1589  {
1590  return str + ", " + assemblyName;
1591  }
1592  catch
1593  {
1594  }
1595  return null;
1596  }
1597 
1598  [SecurityCritical]
1599  internal static string InternalGetTypeNameFromQualifiedTypeName(string qualifiedTypeName)
1600  {
1601  if (qualifiedTypeName == null)
1602  {
1603  throw new ArgumentNullException("qualifiedTypeName");
1604  }
1605  string text = InternalGetClrTypeNameFromQualifiedTypeName(qualifiedTypeName);
1606  if (text != null)
1607  {
1608  return text;
1609  }
1610  int num = IsSoapType(qualifiedTypeName);
1611  if (num != -1)
1612  {
1613  string xmlTypeName = qualifiedTypeName.Substring(5, num - 5);
1614  string xmlTypeNamespace = qualifiedTypeName.Substring(num + 2, qualifiedTypeName.Length - (num + 2));
1615  text = InternalGetSoapTypeNameFromQualifiedTypeName(xmlTypeName, xmlTypeNamespace);
1616  if (text != null)
1617  {
1618  return text;
1619  }
1620  }
1621  return qualifiedTypeName;
1622  }
1623 
1624  [SecurityCritical]
1625  internal static RuntimeType InternalGetTypeFromQualifiedTypeName(string qualifiedTypeName, bool partialFallback)
1626  {
1627  if (qualifiedTypeName == null)
1628  {
1629  throw new ArgumentNullException("qualifiedTypeName");
1630  }
1631  string text = InternalGetClrTypeNameFromQualifiedTypeName(qualifiedTypeName);
1632  if (text != null)
1633  {
1634  return LoadClrTypeWithPartialBindFallback(text, partialFallback);
1635  }
1636  int num = IsSoapType(qualifiedTypeName);
1637  if (num != -1)
1638  {
1639  string text2 = qualifiedTypeName.Substring(5, num - 5);
1640  string xmlTypeNamespace = qualifiedTypeName.Substring(num + 2, qualifiedTypeName.Length - (num + 2));
1641  RuntimeType runtimeType = (RuntimeType)SoapServices.GetInteropTypeFromXmlType(text2, xmlTypeNamespace);
1642  if (runtimeType != null)
1643  {
1644  return runtimeType;
1645  }
1646  text = InternalGetSoapTypeNameFromQualifiedTypeName(text2, xmlTypeNamespace);
1647  if (text != null)
1648  {
1649  return LoadClrTypeWithPartialBindFallback(text, partialFallback: true);
1650  }
1651  }
1652  return LoadClrTypeWithPartialBindFallback(qualifiedTypeName, partialFallback);
1653  }
1654 
1655  [SecurityCritical]
1656  internal static Type InternalGetTypeFromQualifiedTypeName(string qualifiedTypeName)
1657  {
1658  return InternalGetTypeFromQualifiedTypeName(qualifiedTypeName, partialFallback: true);
1659  }
1660 
1661  [MethodImpl(MethodImplOptions.NoInlining)]
1662  private static RuntimeType LoadClrTypeWithPartialBindFallback(string typeName, bool partialFallback)
1663  {
1664  if (!partialFallback)
1665  {
1666  return (RuntimeType)Type.GetType(typeName, throwOnError: false);
1667  }
1668  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
1669  return RuntimeTypeHandle.GetTypeByName(typeName, throwOnError: false, ignoreCase: false, reflectionOnly: false, ref stackMark, loadTypeFromPartialName: true);
1670  }
1671 
1672  [MethodImpl(MethodImplOptions.InternalCall)]
1673  [SecurityCritical]
1674  internal static extern bool CORProfilerTrackRemoting();
1675 
1676  [MethodImpl(MethodImplOptions.InternalCall)]
1677  [SecurityCritical]
1678  internal static extern bool CORProfilerTrackRemotingCookie();
1679 
1680  [MethodImpl(MethodImplOptions.InternalCall)]
1681  [SecurityCritical]
1682  internal static extern bool CORProfilerTrackRemotingAsync();
1683 
1684  [MethodImpl(MethodImplOptions.InternalCall)]
1685  [SecurityCritical]
1686  internal static extern void CORProfilerRemotingClientSendingMessage(out Guid id, bool fIsAsync);
1687 
1688  [MethodImpl(MethodImplOptions.InternalCall)]
1689  [SecurityCritical]
1690  internal static extern void CORProfilerRemotingClientReceivingReply(Guid id, bool fIsAsync);
1691 
1692  [MethodImpl(MethodImplOptions.InternalCall)]
1693  [SecurityCritical]
1694  internal static extern void CORProfilerRemotingServerReceivingMessage(Guid id, bool fIsAsync);
1695 
1696  [MethodImpl(MethodImplOptions.InternalCall)]
1697  [SecurityCritical]
1698  internal static extern void CORProfilerRemotingServerSendingReply(out Guid id, bool fIsAsync);
1699 
1702  [SecurityCritical]
1703  [Conditional("REMOTING_PERF")]
1704  [Obsolete("Use of this method is not recommended. The LogRemotingStage existed for internal diagnostic purposes only.")]
1705  public static void LogRemotingStage(int stage)
1706  {
1707  }
1708 
1709  [MethodImpl(MethodImplOptions.InternalCall)]
1710  [SecurityCritical]
1711  internal static extern void ResetInterfaceCache(object proxy);
1712  }
1713 }
static Thread CurrentThread
Gets the currently running thread.
Definition: Thread.cs:134
Discovers the attributes of a parameter and provides access to parameter metadata.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
MethodBase MethodBase
Gets the T:System.Reflection.MethodBase of the called method.
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
Indicates that the current interface implementer is a reference to another object.
static void SetObjectUriForMarshal(MarshalByRefObject obj, string uri)
Sets the URI for the subsequent call to the M:System.Runtime.Remoting.RemotingServices....
LeaseState
Indicates the possible lease states of a lifetime lease.
Definition: LeaseState.cs:8
Defines the method call return message interface.
Represents the construction call request of an object.
static Context DefaultContext
Gets the default context for the current application domain.
Definition: Context.cs:78
static bool IsMethodOverloaded(IMethodMessage msg)
Returns a Boolean value that indicates whether the method in the given message is overloaded.
static ObjRef Marshal(MarshalByRefObject Obj, string ObjURI, Type RequestedType)
Takes a T:System.MarshalByRefObject and converts it into an instance of the T:System....
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a specified T:System.Runtime.Serialization.SerializationInfo with the data needed to serial...
Definition: ObjRef.cs:276
static void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
Serializes the specified marshal by reference object into the provided T:System.Runtime....
Defines the method call message interface.
static Type GetServerTypeForUri(string URI)
Returns the T:System.Type of the object with the specified URI.
object DeserializeMethodResponse(Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
Deserializes a response to a remote method call from the provided T:System.IO.Stream.
bool IsInterface
Gets a value indicating whether the T:System.Type is an interface; that is, not a class or a value ty...
Definition: Type.cs:426
Discovers the attributes of a class constructor and provides access to constructor metadata.
Provides base functionality for proxies.
Definition: RealProxy.cs:22
void Serialize(Stream serializationStream, object graph)
Serializes the object, or graph of objects with the specified top (root), to the given stream.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
abstract string AssemblyQualifiedName
Gets the assembly-qualified name of the type, which includes the name of the assembly from which this...
Definition: Type.cs:171
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
virtual Type ReturnType
Gets the return type of this method.
Definition: MethodInfo.cs:23
Provides a mechanism that synchronizes access to objects.
Definition: Monitor.cs:13
delegate void AsyncCallback(IAsyncResult ar)
References a method to be called when a corresponding asynchronous operation completes.
static ObjRef GetObjRefForProxy(MarshalByRefObject obj)
Returns the T:System.Runtime.Remoting.ObjRef that represents the remote object from the specified pro...
Stores all relevant information required to generate a proxy in order to communicate with a remote ob...
Definition: ObjRef.cs:19
Defines an environment for the objects that are resident inside it and for which a policy can be enfo...
Definition: Context.cs:14
bool IsIn
Gets a value indicating whether this is an input parameter.
ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
Searches for a constructor whose parameters match the specified argument types and modifiers,...
Definition: Type.cs:1378
static IMethodReturnMessage ExecuteMessage(MarshalByRefObject target, IMethodCallMessage reqMsg)
Connects to the specified remote object, and executes the provided T:System.Runtime....
FormatterAssemblyStyle AssemblyFormat
Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
object Deserialize(Stream serializationStream)
Deserializes the specified stream into an object graph.
FormatterAssemblyStyle
Indicates the method that will be used during deserialization for locating and loading assemblies.
static IMessageSink GetEnvoyChainForProxy(MarshalByRefObject obj)
Returns a chain of envoy sinks that should be used when sending messages to the remote object represe...
StreamingContext Context
Gets or sets the T:System.Runtime.Serialization.StreamingContext for this formatter.
Selects the remoting surrogate that can be used to serialize an object that derives from a T:System....
Represents type declarations for class types, interface types, array types, value types,...
Definition: TypeInfo.cs:11
Serializes and deserializes an object, or an entire graph of connected objects, in binary format.
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
static ObjRef Marshal(MarshalByRefObject Obj)
Takes a T:System.MarshalByRefObject, registers it with the remoting infrastructure,...
virtual byte [] GetBuffer()
Returns the array of unsigned bytes from which this stream was created.
static void LogRemotingStage(int stage)
Logs the stage in a remoting exchange to an external debugger.
virtual ObjRef CreateObjRef(Type requestedType)
Creates an object that contains all the relevant information required to generate a proxy used to com...
CallingConventions
Defines the valid calling conventions for a method.
SecurityAction
Specifies the security actions that can be performed using declarative security.
static void Enter(object obj)
Acquires an exclusive lock on the specified object.
Identifies a T:System.Runtime.Remoting.Messaging.IMethodReturnMessage that is returned after attempti...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents the status of an asynchronous operation.
Definition: IAsyncResult.cs:9
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
Definition: MemoryStream.cs:13
static bool IsObjectOutOfAppDomain(object tp)
Returns a Boolean value that indicates whether the object specified by the given transparent proxy is...
static MethodBase GetMethodBaseFromMethodMessage(IMethodMessage msg)
Returns the method base from the given T:System.Runtime.Remoting.Messaging.IMethodMessage.
static bool IsObjectOutOfContext(object tp)
Returns a Boolean value that indicates whether the object represented by the given proxy is contained...
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
Defines the underlying structure of all code access permissions.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
static object Connect(Type classToProxy, string url, object data)
Creates a proxy for a well-known object, given the T:System.Type, URL, and channel-specific data.
static void Exit(object obj)
Releases an exclusive lock on the specified object.
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
Indicates that an object type requires a custom proxy.
Provides static methods to aid with remoting channel registration, resolution, and URL discovery....
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
static GCHandle Alloc(object value)
Allocates a F:System.Runtime.InteropServices.GCHandleType.Normal handle for the specified object.
Definition: GCHandle.cs:98
Defines utility methods for use by the .NET Framework remoting infrastructure.
object MethodSignature
Gets an object containing the method signature.
static object Connect(Type classToProxy, string url)
Creates a proxy for a well-known object, given the T:System.Type and URL.
Provides information about methods and constructors.
Definition: MethodBase.cs:19
Type DeclaringType
Provides COM objects with version-independent access to the P:System.Reflection.MemberInfo....
Definition: _MethodInfo.cs:31
Provides a way to access a managed object from unmanaged memory.
Definition: GCHandle.cs:10
string TypeName
Gets the full T:System.Type name of the specific object that the call is destined for.
MethodImplOptions
Defines the details of how a method is implemented.
static AppDomain GetDomain()
Returns the current domain in which the current thread is running.
Definition: Thread.cs:1096
virtual object GetTransparentProxy()
Returns the transparent proxy for the current instance of T:System.Runtime.Remoting....
Definition: RealProxy.cs:528
Defines the method message interface.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
Searches for the specified method whose parameters match the specified argument types and modifiers,...
Definition: Type.cs:1488
static string GetObjectUri(MarshalByRefObject obj)
Retrieves the URI for the specified object.
static object Unmarshal(ObjRef objectRef, bool fRefine)
Takes a T:System.Runtime.Remoting.ObjRef and creates a proxy object out of it, refining it to the typ...
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
static bool IsTransparentProxy(object proxy)
Returns a Boolean value that indicates whether the given object is a transparent proxy or a real obje...
string Uri
Gets the URI of the specific object that the call is destined for.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
void Free()
Releases a T:System.Runtime.InteropServices.GCHandle.
Definition: GCHandle.cs:119
static RealProxy GetRealProxy(object proxy)
Returns the real proxy backing the specified transparent proxy.
static bool IsOneWay(MethodBase method)
Returns a Boolean value that indicates whether the client that called the method specified in the giv...
static ObjRef Marshal(MarshalByRefObject Obj, string URI)
Converts the given T:System.MarshalByRefObject into an instance of the T:System.Runtime....
static string GetSessionIdForMethodMessage(IMethodMessage msg)
Retrieves a session ID for a message.
static object Unmarshal(ObjRef objectRef)
Takes a T:System.Runtime.Remoting.ObjRef and creates a proxy object out of it.
virtual RealProxy CreateProxy(ObjRef objRef, Type serverType, object serverObject, Context serverContext)
Creates an instance of a remoting proxy for a remote object described by the specified T:System....
ISurrogateSelector SurrogateSelector
Gets or sets a T:System.Runtime.Serialization.ISurrogateSelector that controls type substitution duri...
bool IsMarshalByRef
Gets a value indicating whether the T:System.Type is marshaled by reference.
Definition: Type.cs:728
Defines the interface for a message sink.
Definition: IMessageSink.cs:8
bool IsOut
Gets a value indicating whether this is an output parameter.
static object GetLifetimeService(MarshalByRefObject obj)
Returns a lifetime service object that controls the lifetime policy of the specified object.
virtual Type ParameterType
Gets the Type of this parameter.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
string Name
Provides COM objects with version-independent access to the P:System.Reflection.MemberInfo....
Definition: _MethodInfo.cs:24
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
StreamingContextStates
Defines a set of flags that specifies the source or destination context for the stream during seriali...
static bool Disconnect(MarshalByRefObject obj)
Stops an object from receiving any further messages through the registered remoting channels.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
ConstructorInfo [] GetConstructors()
Returns all the public constructors defined for the current T:System.Type.
Definition: Type.cs:1458
bool IsByRef
Gets a value indicating whether the T:System.Type is passed by reference.
Definition: Type.cs:664
A conditional operation, such as a > b ? a : b in C# or If(a > b, a, b) in Visual Basic.
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides several methods for using and publishing remoted objects and proxies. This class cannot be i...
The exception that is thrown when binding to a member results in more than one member matching the bi...
Provides static methods to aid with the implementation of a T:System.Runtime.Serialization....
static Context CurrentContext
Gets the current context in which the thread is executing.
Definition: Thread.cs:285
string MethodName
Gets the name of the invoked method.
static IChannel [] RegisteredChannels
Gets a list of currently registered channels.
The exception that is thrown when something has gone wrong during remoting.
Provides a way to register, unregister, and obtain a list of tracking handlers.
abstract ParameterInfo [] GetParameters()
When overridden in a derived class, gets the parameters of the specified method or constructor.
virtual ObjRef CreateObjRef(Type requestedType)
Creates an T:System.Runtime.Remoting.ObjRef for the specified object type, and registers it with the ...
Definition: RealProxy.cs:321
Provides a set of static methods and properties that provide support for compilers....
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
Enables access to objects across application domain boundaries in applications that support remoting.
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18