2 // System.Runtime.Remoting.ObjRef.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Dietmar Maurer (dietmar@ximian.com)
7 // Lluis Sanchez Gual (lluis@ideary.com)
10 // (C) Ximian, Inc. http://www.ximian.com
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Runtime.Serialization;
39 using System.Runtime.Remoting.Channels;
40 using System.Runtime.Remoting.Messaging;
41 using System.Runtime.Remoting.Proxies;
43 using System.Runtime.ConstrainedExecution;
45 namespace System.Runtime.Remoting {
48 [System.Runtime.InteropServices.ComVisible (true)]
49 public class ObjRef : IObjectReference, ISerializable
51 IChannelInfo channel_info;
53 IRemotingTypeInfo typeInfo;
58 static int MarshalledObjectRef = 1;
59 static int WellKnowObjectRef = 2;
63 // no idea why this needs to be public
68 internal ObjRef (string uri, IChannelInfo cinfo)
71 this.channel_info = cinfo;
74 internal ObjRef DeserializeInTheCurrentDomain (int domainId, byte[] tInfo)
76 string local_uri = string.Copy (this.uri);
77 ChannelInfo cinfo = new ChannelInfo (new CrossAppDomainData (domainId));
78 ObjRef res = new ObjRef (local_uri, cinfo);
79 IRemotingTypeInfo typeInfo = (IRemotingTypeInfo)CADSerializer.DeserializeObjectSafe (tInfo);
80 res.typeInfo = typeInfo;
84 internal byte[] SerializeType ()
86 // FIXME: Assert self and typeinfo in same domain
88 throw new Exception ("Attempt to serialize a null TypeInfo.");
90 MemoryStream stm = CADSerializer.SerializeObject (typeInfo);
91 return stm.GetBuffer ();
94 internal ObjRef (ObjRef o, bool unmarshalAsProxy)
96 channel_info = o.channel_info;
99 typeInfo = o.typeInfo;
100 envoyInfo = o.envoyInfo;
102 if (unmarshalAsProxy) flags |= MarshalledObjectRef;
105 public ObjRef (MarshalByRefObject o, Type requestedType)
108 throw new ArgumentNullException ("o");
110 if (requestedType == null)
111 throw new ArgumentNullException ("requestedType");
113 // The ObjRef can only be constructed if the given o
114 // has already been marshalled using RemotingServices.Marshall
116 uri = RemotingServices.GetObjectUri (o);
117 typeInfo = new TypeInfo (requestedType);
119 if (!requestedType.IsInstanceOfType (o))
120 throw new RemotingException ("The server object type cannot be cast to the requested type " + requestedType.FullName);
125 internal ObjRef (Type type, string url, object remoteChannelData)
128 typeInfo = new TypeInfo(type);
130 if (remoteChannelData != null)
131 channel_info = new ChannelInfo (remoteChannelData);
133 flags |= WellKnowObjectRef;
136 protected ObjRef (SerializationInfo info, StreamingContext context)
138 SerializationInfoEnumerator en = info.GetEnumerator();
139 // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
141 bool marshalledValue = true;
143 while (en.MoveNext ()) {
146 uri = (string)en.Value;
149 typeInfo = (IRemotingTypeInfo)en.Value;
152 channel_info = (IChannelInfo)en.Value;
155 envoyInfo = (IEnvoyInfo)en.Value;
157 case "fIsMarshalled":
161 status = ((IConvertible) o).ToInt32(null);
166 marshalledValue = false;
169 flags = Convert.ToInt32 (en.Value);
172 throw new NotSupportedException ();
175 if (marshalledValue) flags |= MarshalledObjectRef;
178 internal bool IsPossibleToCAD ()
180 // we should check if this obj ref belongs to a cross app context.
182 // Return false. If not, serialization of this ObjRef will not work
187 internal bool IsReferenceToWellKnow
189 get { return (flags & WellKnowObjectRef) > 0; }
192 public virtual IChannelInfo ChannelInfo {
193 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
199 channel_info = value;
203 public virtual IEnvoyInfo EnvoyInfo {
212 public virtual IRemotingTypeInfo TypeInfo {
221 public virtual string URI {
230 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
232 info.SetType (GetType());
233 info.AddValue ("uri", uri);
234 info.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
235 info.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
236 info.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
237 info.AddValue ("objrefFlags", flags);
240 public virtual object GetRealObject (StreamingContext context)
242 if ((flags & MarshalledObjectRef) > 0)
243 return RemotingServices.Unmarshal (this);
248 public bool IsFromThisAppDomain ()
250 Identity identity = RemotingServices.GetIdentityForUri (uri);
251 if (identity == null) return false; // URI not registered in this domain
253 return identity.IsFromThisAppDomain;
256 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
257 public bool IsFromThisProcess ()
259 foreach (object data in channel_info.ChannelData)
261 if (data is CrossAppDomainData)
263 string refProcId = ((CrossAppDomainData)data).ProcessID;
264 return (refProcId == RemotingConfiguration.ProcessId);
271 internal void UpdateChannelInfo()
273 channel_info = new ChannelInfo ();
276 internal Type ServerType
280 if (_serverType == null) _serverType = Type.GetType (typeInfo.TypeName);
285 internal void SetDomainID (int id)