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;
31 static int MarshalledObjectRef = 1;
32 static int WellKnowObjectRef = 2;
36 // no idea why this needs to be public
41 internal ObjRef (string typeName, string uri, IChannelInfo cinfo)
45 typeInfo = new TypeInfo (Type.GetType (typeName));
48 internal ObjRef (ObjRef o, bool unmarshalAsProxy) {
49 channel_info = o.channel_info;
52 typeInfo = o.typeInfo;
53 envoyInfo = o.envoyInfo;
55 if (unmarshalAsProxy) flags |= MarshalledObjectRef;
58 public ObjRef (MarshalByRefObject mbr, Type type)
61 throw new ArgumentNullException ("mbr");
64 throw new ArgumentNullException ("type");
66 // The ObjRef can only be constructed if the given mbr
67 // has already been marshalled using RemotingServices.Marshall
69 uri = RemotingServices.GetObjectUri(mbr);
70 typeInfo = new TypeInfo(type);
72 if (!typeInfo.CanCastTo(mbr.GetType(), mbr))
73 throw new RemotingException ("The server object type cannot be cast to the requested type " + type.FullName + ".");
78 internal ObjRef (Type type, string url, object remoteChannelData)
81 typeInfo = new TypeInfo(type);
83 if (remoteChannelData != null)
84 channel_info = new ChannelInfo (remoteChannelData);
86 flags |= WellKnowObjectRef;
89 protected ObjRef (SerializationInfo si, StreamingContext sc)
91 SerializationInfoEnumerator en = si.GetEnumerator();
92 // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
94 bool marshalledValue = true;
96 while (en.MoveNext ()) {
99 uri = (string)en.Value;
102 typeInfo = (IRemotingTypeInfo)en.Value;
105 channel_info = (IChannelInfo)en.Value;
108 envoyInfo = (IEnvoyInfo)en.Value;
110 case "fIsMarshalled":
114 status = ((IConvertible) o).ToInt32(null);
119 marshalledValue = false;
122 flags = Convert.ToInt32 (en.Value);
125 throw new NotSupportedException ();
128 if (marshalledValue) flags |= MarshalledObjectRef;
131 internal bool IsPossibleToCAD ()
133 // we should check if this obj ref belongs to a cross app context.
135 // Return false. If not, serialization of this ObjRef will not work
140 internal bool IsReferenceToWellKnow
142 get { return (flags & WellKnowObjectRef) > 0; }
145 public virtual IChannelInfo ChannelInfo {
152 channel_info = value;
156 public virtual IEnvoyInfo EnvoyInfo {
165 public virtual IRemotingTypeInfo TypeInfo {
174 public virtual string URI {
183 public virtual void GetObjectData (SerializationInfo si, StreamingContext sc)
185 si.SetType (GetType());
186 si.AddValue ("uri", uri);
187 si.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
188 si.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
189 si.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
190 si.AddValue ("objrefFlags", flags);
193 public virtual object GetRealObject (StreamingContext sc)
195 if ((flags & MarshalledObjectRef) > 0)
196 return RemotingServices.Unmarshal (this);
201 public bool IsFromThisAppDomain ()
203 Identity identity = RemotingServices.GetIdentityForUri (uri);
204 if (identity == null) return false; // URI not registered in this domain
206 return identity.IsFromThisAppDomain;
209 public bool IsFromThisProcess ()
211 foreach (object data in channel_info.ChannelData)
213 if (data is CrossAppDomainData)
215 string refProcId = ((CrossAppDomainData)data).ProcessID;
216 return (refProcId == RemotingConfiguration.ProcessId);
223 internal void UpdateChannelInfo()
225 channel_info = new ChannelInfo ();
228 internal Type ServerType
232 if (_serverType == null) _serverType = Type.GetType (typeInfo.TypeName);