ObjRef.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // System.Runtime.Remoting.ObjRef.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Dietmar Maurer ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. // Patrik Torstensson
  9. //
  10. // (C) Ximian, Inc. http://www.ximian.com
  11. //
  12. using System;
  13. using System.Runtime.Serialization;
  14. using System.Runtime.Remoting.Channels;
  15. using System.Runtime.Remoting.Messaging;
  16. using System.Runtime.Remoting.Proxies;
  17. namespace System.Runtime.Remoting {
  18. [Serializable]
  19. public class ObjRef : IObjectReference, ISerializable
  20. {
  21. IChannelInfo channel_info;
  22. string uri;
  23. IRemotingTypeInfo typeInfo;
  24. IEnvoyInfo envoyInfo;
  25. int flags;
  26. Type _serverType;
  27. static int MarshalledObjectRef = 1;
  28. static int WellKnowObjectRef = 2;
  29. public ObjRef ()
  30. {
  31. // no idea why this needs to be public
  32. UpdateChannelInfo();
  33. }
  34. internal ObjRef (ObjRef o, bool unmarshalAsProxy) {
  35. channel_info = o.channel_info;
  36. uri = o.uri;
  37. typeInfo = o.typeInfo;
  38. envoyInfo = o.envoyInfo;
  39. flags = o.flags;
  40. if (unmarshalAsProxy) flags |= MarshalledObjectRef;
  41. }
  42. public ObjRef (MarshalByRefObject mbr, Type type)
  43. {
  44. if (mbr == null)
  45. throw new ArgumentNullException ("mbr");
  46. if (type == null)
  47. throw new ArgumentNullException ("type");
  48. // The ObjRef can only be constructed if the given mbr
  49. // has already been marshalled using RemotingServices.Marshall
  50. uri = RemotingServices.GetObjectUri(mbr);
  51. typeInfo = new TypeInfo(type);
  52. if (!typeInfo.CanCastTo(mbr.GetType(), mbr))
  53. throw new RemotingException ("The server object type cannot be cast to the requested type " + type.FullName + ".");
  54. UpdateChannelInfo();
  55. }
  56. internal ObjRef (Type type, string url, object remoteChannelData)
  57. {
  58. uri = url;
  59. typeInfo = new TypeInfo(type);
  60. if (remoteChannelData != null)
  61. channel_info = new ChannelInfo (remoteChannelData);
  62. flags |= WellKnowObjectRef;
  63. }
  64. protected ObjRef (SerializationInfo si, StreamingContext sc)
  65. {
  66. SerializationInfoEnumerator en = si.GetEnumerator();
  67. // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
  68. bool marshalledValue = true;
  69. while (en.MoveNext ()) {
  70. switch (en.Name) {
  71. case "uri":
  72. uri = (string)en.Value;
  73. break;
  74. case "typeInfo":
  75. typeInfo = (IRemotingTypeInfo)en.Value;
  76. break;
  77. case "channelInfo":
  78. channel_info = (IChannelInfo)en.Value;
  79. break;
  80. case "envoyInfo":
  81. envoyInfo = (IEnvoyInfo)en.Value;
  82. break;
  83. case "fIsMarshalled":
  84. int status;
  85. Object o = en.Value;
  86. if (o.GetType().Equals(typeof(String)))
  87. status = ((IConvertible) o).ToInt32(null);
  88. else
  89. status = (int) o;
  90. if (status == 0)
  91. marshalledValue = false;
  92. break;
  93. case "objrefFlags":
  94. flags = Convert.ToInt32 (en.Value);
  95. break;
  96. default:
  97. throw new NotSupportedException ();
  98. }
  99. }
  100. if (marshalledValue) flags |= MarshalledObjectRef;
  101. }
  102. internal bool IsPossibleToCAD ()
  103. {
  104. // we should check if this obj ref belongs to a cross app context.
  105. // Return false. If not, serialization of this ObjRef will not work
  106. // on the target AD.
  107. return false;
  108. }
  109. public bool IsReferenceToWellKnow
  110. {
  111. get { return (flags & WellKnowObjectRef) > 0; }
  112. }
  113. public virtual IChannelInfo ChannelInfo {
  114. get {
  115. return channel_info;
  116. }
  117. set {
  118. channel_info = value;
  119. }
  120. }
  121. public virtual IEnvoyInfo EnvoyInfo {
  122. get {
  123. return envoyInfo;
  124. }
  125. set {
  126. envoyInfo = value;
  127. }
  128. }
  129. public virtual IRemotingTypeInfo TypeInfo {
  130. get {
  131. return typeInfo;
  132. }
  133. set {
  134. typeInfo = value;
  135. }
  136. }
  137. public virtual string URI {
  138. get {
  139. return uri;
  140. }
  141. set {
  142. uri = value;
  143. }
  144. }
  145. public virtual void GetObjectData (SerializationInfo si, StreamingContext sc)
  146. {
  147. si.SetType (GetType());
  148. si.AddValue ("uri", uri);
  149. si.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
  150. si.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
  151. si.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
  152. si.AddValue ("objrefFlags", flags);
  153. }
  154. public virtual object GetRealObject (StreamingContext sc)
  155. {
  156. if ((flags & MarshalledObjectRef) > 0)
  157. return RemotingServices.Unmarshal (this);
  158. else
  159. return this;
  160. }
  161. public bool IsFromThisAppDomain ()
  162. {
  163. Identity identity = RemotingServices.GetIdentityForUri (uri);
  164. if (identity == null) return false; // URI not registered in this domain
  165. return identity.IsFromThisAppDomain;
  166. }
  167. [MonoTODO]
  168. public bool IsFromThisProcess ()
  169. {
  170. // as yet we do not consider this optimization
  171. return false;
  172. }
  173. internal void UpdateChannelInfo()
  174. {
  175. channel_info = new ChannelInfo ();
  176. }
  177. internal Type ServerType
  178. {
  179. get
  180. {
  181. if (_serverType == null) _serverType = Type.GetType (typeInfo.TypeName);
  182. return _serverType;
  183. }
  184. }
  185. }
  186. }