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.
37 using System.Runtime.Serialization;
38 using System.Runtime.Remoting.Channels;
39 using System.Runtime.Remoting.Messaging;
40 using System.Runtime.Remoting.Proxies;
42 namespace System.Runtime.Remoting {
45 public class ObjRef : IObjectReference, ISerializable
47 IChannelInfo channel_info;
49 IRemotingTypeInfo typeInfo;
54 static int MarshalledObjectRef = 1;
55 static int WellKnowObjectRef = 2;
59 // no idea why this needs to be public
64 internal ObjRef (string typeName, string uri, IChannelInfo cinfo)
68 typeInfo = new TypeInfo (Type.GetType (typeName));
71 internal ObjRef (ObjRef o, bool unmarshalAsProxy) {
72 channel_info = o.channel_info;
75 typeInfo = o.typeInfo;
76 envoyInfo = o.envoyInfo;
78 if (unmarshalAsProxy) flags |= MarshalledObjectRef;
81 public ObjRef (MarshalByRefObject mbr, Type type)
84 throw new ArgumentNullException ("mbr");
87 throw new ArgumentNullException ("type");
89 // The ObjRef can only be constructed if the given mbr
90 // has already been marshalled using RemotingServices.Marshall
92 uri = RemotingServices.GetObjectUri(mbr);
93 typeInfo = new TypeInfo(type);
95 if (!type.IsAssignableFrom (mbr.GetType()))
96 throw new RemotingException ("The server object type cannot be cast to the requested type " + type.FullName + ".");
101 internal ObjRef (Type type, string url, object remoteChannelData)
104 typeInfo = new TypeInfo(type);
106 if (remoteChannelData != null)
107 channel_info = new ChannelInfo (remoteChannelData);
109 flags |= WellKnowObjectRef;
112 protected ObjRef (SerializationInfo si, StreamingContext sc)
114 SerializationInfoEnumerator en = si.GetEnumerator();
115 // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
117 bool marshalledValue = true;
119 while (en.MoveNext ()) {
122 uri = (string)en.Value;
125 typeInfo = (IRemotingTypeInfo)en.Value;
128 channel_info = (IChannelInfo)en.Value;
131 envoyInfo = (IEnvoyInfo)en.Value;
133 case "fIsMarshalled":
137 status = ((IConvertible) o).ToInt32(null);
142 marshalledValue = false;
145 flags = Convert.ToInt32 (en.Value);
148 throw new NotSupportedException ();
151 if (marshalledValue) flags |= MarshalledObjectRef;
154 internal bool IsPossibleToCAD ()
156 // we should check if this obj ref belongs to a cross app context.
158 // Return false. If not, serialization of this ObjRef will not work
163 internal bool IsReferenceToWellKnow
165 get { return (flags & WellKnowObjectRef) > 0; }
168 public virtual IChannelInfo ChannelInfo {
175 channel_info = value;
179 public virtual IEnvoyInfo EnvoyInfo {
188 public virtual IRemotingTypeInfo TypeInfo {
197 public virtual string URI {
206 public virtual void GetObjectData (SerializationInfo si, StreamingContext sc)
208 si.SetType (GetType());
209 si.AddValue ("uri", uri);
210 si.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
211 si.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
212 si.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
213 si.AddValue ("objrefFlags", flags);
216 public virtual object GetRealObject (StreamingContext sc)
218 if ((flags & MarshalledObjectRef) > 0)
219 return RemotingServices.Unmarshal (this);
224 public bool IsFromThisAppDomain ()
226 Identity identity = RemotingServices.GetIdentityForUri (uri);
227 if (identity == null) return false; // URI not registered in this domain
229 return identity.IsFromThisAppDomain;
232 public bool IsFromThisProcess ()
234 foreach (object data in channel_info.ChannelData)
236 if (data is CrossAppDomainData)
238 string refProcId = ((CrossAppDomainData)data).ProcessID;
239 return (refProcId == RemotingConfiguration.ProcessId);
246 internal void UpdateChannelInfo()
248 channel_info = new ChannelInfo ();
251 internal Type ServerType
255 if (_serverType == null) _serverType = Type.GetType (typeInfo.TypeName);