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 using System.Runtime.Serialization;
15 using System.Runtime.Remoting.Channels;
16 using System.Runtime.Remoting.Messaging;
17 using System.Runtime.Remoting.Proxies;
19 namespace System.Runtime.Remoting {
22 public class ObjRef : IObjectReference, ISerializable
24 IChannelInfo channel_info;
26 IRemotingTypeInfo typeInfo;
30 static int MarshalledObjectRef = 1;
31 static int WellKnowObjectRef = 2;
35 // no idea why this needs to be public
40 internal ObjRef (ObjRef o, bool unmarshalAsProxy) {
41 channel_info = o.channel_info;
44 typeInfo = o.typeInfo;
45 envoyInfo = o.envoyInfo;
47 if (unmarshalAsProxy) flags |= MarshalledObjectRef;
50 public ObjRef (MarshalByRefObject mbr, Type type)
53 throw new ArgumentNullException ("mbr");
56 throw new ArgumentNullException ("type");
58 // The ObjRef can only be constructed if the given mbr
59 // has already been marshalled using RemotingServices.Marshall
61 uri = RemotingServices.GetObjectUri(mbr);
62 typeInfo = new TypeInfo(type);
64 if (!typeInfo.CanCastTo(mbr.GetType(), mbr))
65 throw new RemotingException ("The server object type cannot be cast to the requested type " + type.FullName + ".");
70 internal ObjRef (Type type, string url, object remoteChannelData)
73 typeInfo = new TypeInfo(type);
75 if (remoteChannelData != null)
76 channel_info = new ChannelInfoStore (remoteChannelData);
78 flags |= WellKnowObjectRef;
81 protected ObjRef (SerializationInfo si, StreamingContext sc)
83 SerializationInfoEnumerator en = si.GetEnumerator();
84 // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
86 bool marshalledValue = true;
88 while (en.MoveNext ()) {
91 uri = (string)en.Value;
94 typeInfo = (IRemotingTypeInfo)en.Value;
97 channel_info = (IChannelInfo)en.Value;
100 envoyInfo = (IEnvoyInfo)en.Value;
102 case "fIsMarshalled":
105 if (o.GetType().Equals(typeof(String)))
106 status = ((IConvertible) o).ToInt32(null);
111 marshalledValue = false;
114 flags = (int) en.Value;
117 throw new NotSupportedException ();
120 if (marshalledValue) flags |= MarshalledObjectRef;
123 internal bool IsPossibleToCAD ()
125 // we should check if this obj ref belongs to a cross app context.
127 // Return false. If not, serialization of this ObjRef will not work
132 public bool IsReferenceToWellKnow
134 get { return (flags & WellKnowObjectRef) > 0; }
137 public virtual IChannelInfo ChannelInfo {
144 channel_info = value;
149 public virtual IEnvoyInfo EnvoyInfo {
151 throw new NotImplementedException ();
154 throw new NotImplementedException ();
159 public virtual IRemotingTypeInfo TypeInfo {
168 public virtual string URI {
177 public virtual void GetObjectData (SerializationInfo si, StreamingContext sc)
179 si.SetType (GetType());
180 si.AddValue ("uri", uri);
181 si.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
182 si.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
183 si.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
184 si.AddValue ("objrefFlags", flags);
187 public virtual object GetRealObject (StreamingContext sc)
189 if ((flags & MarshalledObjectRef) > 0)
190 return RemotingServices.Unmarshal (this);
195 public bool IsFromThisAppDomain ()
197 Identity identity = RemotingServices.GetIdentityForUri (uri);
198 if (identity == null) return false; // URI not registered in this domain
200 return identity.IsFromThisAppDomain;
204 public bool IsFromThisProcess ()
206 // as yet we do not consider this optimization
210 internal void UpdateChannelInfo()
212 channel_info = new ChannelInfoStore ();