mscorlib(4.0.0.0) API with additions
ObjRef.cs
2 using System.Reflection;
9 using System.Security;
11 
13 {
15  [Serializable]
16  [SecurityCritical]
17  [ComVisible(true)]
18  [SecurityPermission(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
20  {
21  internal const int FLG_MARSHALED_OBJECT = 1;
22 
23  internal const int FLG_WELLKNOWN_OBJREF = 2;
24 
25  internal const int FLG_LITE_OBJREF = 4;
26 
27  internal const int FLG_PROXY_ATTRIBUTE = 8;
28 
29  internal string uri;
30 
31  internal IRemotingTypeInfo typeInfo;
32 
33  internal IEnvoyInfo envoyInfo;
34 
35  internal IChannelInfo channelInfo;
36 
37  internal int objrefFlags;
38 
39  internal GCHandle srvIdentity;
40 
41  internal int domainID;
42 
43  private static Type orType = typeof(ObjRef);
44 
47  public virtual string URI
48  {
49  get
50  {
51  return uri;
52  }
53  set
54  {
55  uri = value;
56  }
57  }
58 
61  public virtual IRemotingTypeInfo TypeInfo
62  {
63  get
64  {
65  return typeInfo;
66  }
67  set
68  {
69  typeInfo = value;
70  }
71  }
72 
75  public virtual IEnvoyInfo EnvoyInfo
76  {
77  get
78  {
79  return envoyInfo;
80  }
81  set
82  {
83  envoyInfo = value;
84  }
85  }
86 
89  public virtual IChannelInfo ChannelInfo
90  {
91  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
92  get
93  {
94  return channelInfo;
95  }
96  set
97  {
98  channelInfo = value;
99  }
100  }
101 
102  internal void SetServerIdentity(GCHandle hndSrvIdentity)
103  {
104  srvIdentity = hndSrvIdentity;
105  }
106 
107  internal GCHandle GetServerIdentity()
108  {
109  return srvIdentity;
110  }
111 
112  internal void SetDomainID(int id)
113  {
114  domainID = id;
115  }
116 
117  internal int GetDomainID()
118  {
119  return domainID;
120  }
121 
122  [SecurityCritical]
123  private ObjRef(ObjRef o)
124  {
125  uri = o.uri;
126  typeInfo = o.typeInfo;
127  envoyInfo = o.envoyInfo;
128  channelInfo = o.channelInfo;
129  objrefFlags = o.objrefFlags;
130  SetServerIdentity(o.GetServerIdentity());
131  SetDomainID(o.GetDomainID());
132  }
133 
137  [SecurityCritical]
138  public ObjRef(MarshalByRefObject o, Type requestedType)
139  {
140  if (o == null)
141  {
142  throw new ArgumentNullException("o");
143  }
144  RuntimeType runtimeType = requestedType as RuntimeType;
145  if (requestedType != null && runtimeType == null)
146  {
147  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
148  }
149  bool fServer;
150  Identity identity = MarshalByRefObject.GetIdentity(o, out fServer);
151  Init(o, identity, runtimeType);
152  }
153 
157  [SecurityCritical]
158  protected ObjRef(SerializationInfo info, StreamingContext context)
159  {
160  string text = null;
161  bool flag = false;
162  SerializationInfoEnumerator enumerator = info.GetEnumerator();
163  while (enumerator.MoveNext())
164  {
165  if (enumerator.Name.Equals("uri"))
166  {
167  uri = (string)enumerator.Value;
168  }
169  else if (enumerator.Name.Equals("typeInfo"))
170  {
171  typeInfo = (IRemotingTypeInfo)enumerator.Value;
172  }
173  else if (enumerator.Name.Equals("envoyInfo"))
174  {
175  envoyInfo = (IEnvoyInfo)enumerator.Value;
176  }
177  else if (enumerator.Name.Equals("channelInfo"))
178  {
179  channelInfo = (IChannelInfo)enumerator.Value;
180  }
181  else if (enumerator.Name.Equals("objrefFlags"))
182  {
183  object value = enumerator.Value;
184  if (value.GetType() == typeof(string))
185  {
186  objrefFlags = ((IConvertible)value).ToInt32(null);
187  }
188  else
189  {
190  objrefFlags = (int)value;
191  }
192  }
193  else if (enumerator.Name.Equals("fIsMarshalled"))
194  {
195  object value2 = enumerator.Value;
196  if (((!(value2.GetType() == typeof(string))) ? ((int)value2) : ((IConvertible)value2).ToInt32(null)) == 0)
197  {
198  flag = true;
199  }
200  }
201  else if (enumerator.Name.Equals("url"))
202  {
203  text = (string)enumerator.Value;
204  }
205  else if (enumerator.Name.Equals("SrvIdentity"))
206  {
207  SetServerIdentity((GCHandle)enumerator.Value);
208  }
209  else if (enumerator.Name.Equals("DomainId"))
210  {
211  SetDomainID((int)enumerator.Value);
212  }
213  }
214  if (!flag)
215  {
216  objrefFlags |= 1;
217  }
218  else
219  {
220  objrefFlags &= -2;
221  }
222  if (text != null)
223  {
224  uri = text;
225  objrefFlags |= 4;
226  }
227  }
228 
229  [SecurityCritical]
230  internal bool CanSmuggle()
231  {
232  if (GetType() != typeof(ObjRef) || IsObjRefLite())
233  {
234  return false;
235  }
236  Type left = null;
237  if (typeInfo != null)
238  {
239  left = typeInfo.GetType();
240  }
241  Type left2 = null;
242  if (channelInfo != null)
243  {
244  left2 = channelInfo.GetType();
245  }
246  if ((left == null || left == typeof(TypeInfo) || left == typeof(DynamicTypeInfo)) && envoyInfo == null && (left2 == null || left2 == typeof(ChannelInfo)))
247  {
248  if (channelInfo != null)
249  {
250  object[] channelData = channelInfo.ChannelData;
251  foreach (object obj in channelData)
252  {
253  if (!(obj is CrossAppDomainData))
254  {
255  return false;
256  }
257  }
258  }
259  return true;
260  }
261  return false;
262  }
263 
264  [SecurityCritical]
265  internal ObjRef CreateSmuggleableCopy()
266  {
267  return new ObjRef(this);
268  }
269 
275  [SecurityCritical]
276  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
277  {
278  if (info == null)
279  {
280  throw new ArgumentNullException("info");
281  }
282  info.SetType(orType);
283  if (!IsObjRefLite())
284  {
285  info.AddValue("uri", uri, typeof(string));
286  info.AddValue("objrefFlags", objrefFlags);
287  info.AddValue("typeInfo", typeInfo, typeof(IRemotingTypeInfo));
288  info.AddValue("envoyInfo", envoyInfo, typeof(IEnvoyInfo));
289  info.AddValue("channelInfo", GetChannelInfoHelper(), typeof(IChannelInfo));
290  }
291  else
292  {
293  info.AddValue("url", uri, typeof(string));
294  }
295  }
296 
297  [SecurityCritical]
298  private IChannelInfo GetChannelInfoHelper()
299  {
300  ChannelInfo channelInfo = this.channelInfo as ChannelInfo;
301  if (channelInfo == null)
302  {
303  return this.channelInfo;
304  }
305  object[] channelData = channelInfo.ChannelData;
306  if (channelData == null)
307  {
308  return channelInfo;
309  }
310  string[] array = (string[])CallContext.GetData("__bashChannelUrl");
311  if (array == null)
312  {
313  return channelInfo;
314  }
315  string value = array[0];
316  string text = array[1];
317  ChannelInfo channelInfo2 = new ChannelInfo();
318  channelInfo2.ChannelData = new object[channelData.Length];
319  for (int i = 0; i < channelData.Length; i++)
320  {
321  channelInfo2.ChannelData[i] = channelData[i];
322  ChannelDataStore channelDataStore = channelInfo2.ChannelData[i] as ChannelDataStore;
323  if (channelDataStore != null)
324  {
325  string[] channelUris = channelDataStore.ChannelUris;
326  if (channelUris != null && channelUris.Length == 1 && channelUris[0].Equals(value))
327  {
328  ChannelDataStore channelDataStore2 = channelDataStore.InternalShallowCopy();
329  channelDataStore2.ChannelUris = new string[1];
330  channelDataStore2.ChannelUris[0] = text;
331  channelInfo2.ChannelData[i] = channelDataStore2;
332  }
333  }
334  }
335  return channelInfo2;
336  }
337 
342  [SecurityCritical]
343  public virtual object GetRealObject(StreamingContext context)
344  {
345  return GetRealObjectHelper();
346  }
347 
348  [SecurityCritical]
349  internal object GetRealObjectHelper()
350  {
351  if (!IsMarshaledObject())
352  {
353  return this;
354  }
355  if (IsObjRefLite())
356  {
357  int num = uri.IndexOf(RemotingConfiguration.ApplicationId);
358  if (num > 0)
359  {
360  uri = uri.Substring(num - 1);
361  }
362  }
363  bool fRefine = !(GetType() == typeof(ObjRef));
364  object ret = RemotingServices.Unmarshal(this, fRefine);
365  return GetCustomMarshaledCOMObject(ret);
366  }
367 
368  [SecurityCritical]
369  private object GetCustomMarshaledCOMObject(object ret)
370  {
371  DynamicTypeInfo dynamicTypeInfo = TypeInfo as DynamicTypeInfo;
372  if (dynamicTypeInfo != null)
373  {
374  object obj = null;
375  IntPtr intPtr = IntPtr.Zero;
377  {
378  try
379  {
380  intPtr = ((__ComObject)ret).GetIUnknown(out bool fIsURTAggregated);
381  if (!(intPtr != IntPtr.Zero))
382  {
383  return ret;
384  }
385  if (fIsURTAggregated)
386  {
387  return ret;
388  }
389  string typeName = TypeInfo.TypeName;
390  string typeName2 = null;
391  string assemName = null;
392  System.Runtime.Remoting.TypeInfo.ParseTypeAndAssembly(typeName, out typeName2, out assemName);
393  Assembly assembly = FormatterServices.LoadAssemblyFromStringNoThrow(assemName);
394  if (assembly == null)
395  {
396  throw new RemotingException(Environment.GetResourceString("Serialization_AssemblyNotFound", assemName));
397  }
398  Type type = assembly.GetType(typeName2, throwOnError: false, ignoreCase: false);
399  if (type != null && !type.IsVisible)
400  {
401  type = null;
402  }
403  obj = Marshal.GetTypedObjectForIUnknown(intPtr, type);
404  if (obj == null)
405  {
406  return ret;
407  }
408  ret = obj;
409  return ret;
410  }
411  finally
412  {
413  if (intPtr != IntPtr.Zero)
414  {
415  Marshal.Release(intPtr);
416  }
417  }
418  }
419  }
420  return ret;
421  }
422 
424  public ObjRef()
425  {
426  objrefFlags = 0;
427  }
428 
429  internal bool IsMarshaledObject()
430  {
431  return (objrefFlags & 1) == 1;
432  }
433 
434  internal void SetMarshaledObject()
435  {
436  objrefFlags |= 1;
437  }
438 
439  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
440  internal bool IsWellKnown()
441  {
442  return (objrefFlags & 2) == 2;
443  }
444 
445  internal void SetWellKnown()
446  {
447  objrefFlags |= 2;
448  }
449 
450  internal bool HasProxyAttribute()
451  {
452  return (objrefFlags & 8) == 8;
453  }
454 
455  internal void SetHasProxyAttribute()
456  {
457  objrefFlags |= 8;
458  }
459 
460  internal bool IsObjRefLite()
461  {
462  return (objrefFlags & 4) == 4;
463  }
464 
465  internal void SetObjRefLite()
466  {
467  objrefFlags |= 4;
468  }
469 
470  [SecurityCritical]
471  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
472  private CrossAppDomainData GetAppDomainChannelData()
473  {
474  int i = 0;
475  CrossAppDomainData crossAppDomainData = null;
476  for (; i < ChannelInfo.ChannelData.Length; i++)
477  {
478  crossAppDomainData = (ChannelInfo.ChannelData[i] as CrossAppDomainData);
479  if (crossAppDomainData != null)
480  {
481  return crossAppDomainData;
482  }
483  }
484  return null;
485  }
486 
489  [SecurityCritical]
490  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
491  public bool IsFromThisProcess()
492  {
493  if (IsWellKnown())
494  {
495  return false;
496  }
497  return GetAppDomainChannelData()?.IsFromThisProcess() ?? false;
498  }
499 
502  [SecurityCritical]
503  public bool IsFromThisAppDomain()
504  {
505  return GetAppDomainChannelData()?.IsFromThisAppDomain() ?? false;
506  }
507 
508  [SecurityCritical]
509  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
510  internal int GetServerDomainId()
511  {
512  if (!IsFromThisProcess())
513  {
514  return 0;
515  }
516  CrossAppDomainData appDomainChannelData = GetAppDomainChannelData();
517  return appDomainChannelData.DomainID;
518  }
519 
520  [SecurityCritical]
521  internal IntPtr GetServerContext(out int domainId)
522  {
523  IntPtr result = IntPtr.Zero;
524  domainId = 0;
525  if (IsFromThisProcess())
526  {
527  CrossAppDomainData appDomainChannelData = GetAppDomainChannelData();
528  domainId = appDomainChannelData.DomainID;
529  if (AppDomain.IsDomainIdValid(appDomainChannelData.DomainID))
530  {
531  result = appDomainChannelData.ContextID;
532  }
533  }
534  return result;
535  }
536 
537  [SecurityCritical]
538  internal void Init(object o, Identity idObj, RuntimeType requestedType)
539  {
540  uri = idObj.URI;
541  MarshalByRefObject tPOrObject = idObj.TPOrObject;
542  RuntimeType runtimeType = null;
543  runtimeType = (RemotingServices.IsTransparentProxy(tPOrObject) ? ((RuntimeType)RemotingServices.GetRealProxy(tPOrObject).GetProxiedType()) : ((RuntimeType)tPOrObject.GetType()));
544  RuntimeType runtimeType2 = (null == requestedType) ? runtimeType : requestedType;
545  if (null != requestedType && !requestedType.IsAssignableFrom(runtimeType) && !typeof(IMessageSink).IsAssignableFrom(runtimeType))
546  {
547  throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_InvalidRequestedType"), requestedType.ToString()));
548  }
549  if (runtimeType.IsCOMObject)
550  {
551  DynamicTypeInfo dynamicTypeInfo = (DynamicTypeInfo)(TypeInfo = new DynamicTypeInfo(runtimeType2));
552  }
553  else
554  {
555  RemotingTypeCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(runtimeType2);
556  TypeInfo = reflectionCachedData.TypeInfo;
557  }
558  if (!idObj.IsWellKnown())
559  {
560  EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);
561  IChannelInfo channelInfo = new ChannelInfo();
562  if (o is AppDomain)
563  {
564  object[] channelData = channelInfo.ChannelData;
565  int num = channelData.Length;
566  object[] array = new object[num];
567  Array.Copy(channelData, array, num);
568  for (int i = 0; i < num; i++)
569  {
570  if (!(array[i] is CrossAppDomainData))
571  {
572  array[i] = null;
573  }
574  }
575  channelInfo.ChannelData = array;
576  }
577  ChannelInfo = channelInfo;
578  if (runtimeType.HasProxyAttribute)
579  {
580  SetHasProxyAttribute();
581  }
582  }
583  else
584  {
585  SetWellKnown();
586  }
587  if (!ShouldUseUrlObjRef())
588  {
589  return;
590  }
591  if (IsWellKnown())
592  {
593  SetObjRefLite();
594  return;
595  }
596  string text = ChannelServices.FindFirstHttpUrlForObject(URI);
597  if (text != null)
598  {
599  URI = text;
600  SetObjRefLite();
601  }
602  }
603 
604  internal static bool ShouldUseUrlObjRef()
605  {
606  return RemotingConfigHandler.UrlObjRefMode;
607  }
608 
609  [SecurityCritical]
610  internal static bool IsWellFormed(ObjRef objectRef)
611  {
612  bool result = true;
613  if (objectRef == null || objectRef.URI == null || (!objectRef.IsWellKnown() && !objectRef.IsObjRefLite() && !(objectRef.GetType() != orType) && objectRef.ChannelInfo == null))
614  {
615  result = false;
616  }
617  return result;
618  }
619  }
620 }
virtual string URI
Gets or sets the URI of the specific object instance.
Definition: ObjRef.cs:48
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.
ObjRef()
Initializes a new instance of the T:System.Runtime.Remoting.ObjRef class with default values.
Definition: ObjRef.cs:424
Indicates that the current interface implementer is a reference to another object.
virtual IChannelInfo ChannelInfo
Gets or sets the T:System.Runtime.Remoting.IChannelInfo for the T:System.Runtime.Remoting....
Definition: ObjRef.cs:90
object Value
Gets the value of the item currently being examined.
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
Definition: __Canon.cs:3
static int Release(IntPtr pUnk)
Decrements the reference count on the specified interface.
Stores all relevant information required to generate a proxy in order to communicate with a remote ob...
Definition: ObjRef.cs:19
ObjRef(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Runtime.Remoting.ObjRef class from serialized data.
Definition: ObjRef.cs:158
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Represents an application domain, which is an isolated environment where applications execute....
Definition: AppDomain.cs:33
override string ToString()
Returns a string containing the name of the current T:System.Globalization.CultureInfo in the format ...
Represents type declarations for class types, interface types, array types, value types,...
Definition: TypeInfo.cs:11
ObjRef(MarshalByRefObject o, Type requestedType)
Initializes a new instance of the T:System.Runtime.Remoting.ObjRef class to reference a specified T:S...
Definition: ObjRef.cs:138
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
Permission to configure Remoting types and channels.
string [] ChannelUris
Gets or sets an array of channel URIs that the current channel maps to.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Provides a set of properties that are carried with the execution code path. This class cannot be inhe...
Definition: CallContext.cs:12
static object GetTypedObjectForIUnknown(IntPtr pUnk, Type t)
Returns a managed object of a specified type that represents a COM object.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
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
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Provides a way to access a managed object from unmanaged memory.
Definition: GCHandle.cs:10
virtual IEnvoyInfo EnvoyInfo
Gets or sets the T:System.Runtime.Remoting.IEnvoyInfo for the T:System.Runtime.Remoting....
Definition: ObjRef.cs:76
bool IsFromThisProcess()
Returns a Boolean value that indicates whether the current T:System.Runtime.Remoting....
Definition: ObjRef.cs:491
virtual IRemotingTypeInfo TypeInfo
Gets or sets the T:System.Runtime.Remoting.IRemotingTypeInfo for the object that the T:System....
Definition: ObjRef.cs:62
Provides type information for an object.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
Provides custom channel information that is carried along with the T:System.Runtime....
Definition: IChannelInfo.cs:8
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
Provides envoy information.
Definition: IEnvoyInfo.cs:9
static object GetData(string name)
Retrieves an object with the specified name from the T:System.Runtime.Remoting.Messaging....
Definition: CallContext.cs:104
virtual object GetRealObject(StreamingContext context)
Returns a reference to the remote object that the T:System.Runtime.Remoting.ObjRef describes.
Definition: ObjRef.cs:343
Stores channel data for the remoting channels.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
Defines the interface for a message sink.
Definition: IMessageSink.cs:8
Specifies that the class can be serialized.
string Name
Gets the name for the item currently being examined.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Defines methods that convert the value of the implementing reference or value type to a common langua...
Definition: IConvertible.cs:9
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides a formatter-friendly mechanism for parsing the data in T:System.Runtime.Serialization....
Provides static methods to aid with the implementation of a T:System.Runtime.Serialization....
bool IsFromThisAppDomain()
Returns a Boolean value that indicates whether the current T:System.Runtime.Remoting....
Definition: ObjRef.cs:503
object [] ChannelData
Gets and sets the channel data for each channel.
Definition: IChannelInfo.cs:14
bool MoveNext()
Updates the enumerator to the next item.
Enables access to objects across application domain boundaries in applications that support remoting.