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;
43 using System.Runtime.ConstrainedExecution;
46 namespace System.Runtime.Remoting {
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 typeName, string uri, IChannelInfo cinfo)
72 typeInfo = new TypeInfo (Type.GetType (typeName, true));
75 internal ObjRef (ObjRef o, bool unmarshalAsProxy) {
76 channel_info = o.channel_info;
79 typeInfo = o.typeInfo;
80 envoyInfo = o.envoyInfo;
82 if (unmarshalAsProxy) flags |= MarshalledObjectRef;
85 public ObjRef (MarshalByRefObject mbr, Type type)
88 throw new ArgumentNullException ("mbr");
91 throw new ArgumentNullException ("type");
93 // The ObjRef can only be constructed if the given mbr
94 // has already been marshalled using RemotingServices.Marshall
96 uri = RemotingServices.GetObjectUri(mbr);
97 typeInfo = new TypeInfo(type);
99 if (!type.IsInstanceOfType (mbr))
100 throw new RemotingException ("The server object type cannot be cast to the requested type " + type.FullName + ".");
105 internal ObjRef (Type type, string url, object remoteChannelData)
108 typeInfo = new TypeInfo(type);
110 if (remoteChannelData != null)
111 channel_info = new ChannelInfo (remoteChannelData);
113 flags |= WellKnowObjectRef;
116 protected ObjRef (SerializationInfo si, StreamingContext sc)
118 SerializationInfoEnumerator en = si.GetEnumerator();
119 // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
121 bool marshalledValue = true;
123 while (en.MoveNext ()) {
126 uri = (string)en.Value;
129 typeInfo = (IRemotingTypeInfo)en.Value;
132 channel_info = (IChannelInfo)en.Value;
135 envoyInfo = (IEnvoyInfo)en.Value;
137 case "fIsMarshalled":
141 status = ((IConvertible) o).ToInt32(null);
146 marshalledValue = false;
149 flags = Convert.ToInt32 (en.Value);
152 throw new NotSupportedException ();
155 if (marshalledValue) flags |= MarshalledObjectRef;
158 internal bool IsPossibleToCAD ()
160 // we should check if this obj ref belongs to a cross app context.
162 // Return false. If not, serialization of this ObjRef will not work
167 internal bool IsReferenceToWellKnow
169 get { return (flags & WellKnowObjectRef) > 0; }
172 public virtual IChannelInfo ChannelInfo {
174 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
181 channel_info = value;
185 public virtual IEnvoyInfo EnvoyInfo {
194 public virtual IRemotingTypeInfo TypeInfo {
203 public virtual string URI {
212 public virtual void GetObjectData (SerializationInfo si, StreamingContext sc)
214 si.SetType (GetType());
215 si.AddValue ("uri", uri);
216 si.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
217 si.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
218 si.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
219 si.AddValue ("objrefFlags", flags);
222 public virtual object GetRealObject (StreamingContext sc)
224 if ((flags & MarshalledObjectRef) > 0)
225 return RemotingServices.Unmarshal (this);
230 public bool IsFromThisAppDomain ()
232 Identity identity = RemotingServices.GetIdentityForUri (uri);
233 if (identity == null) return false; // URI not registered in this domain
235 return identity.IsFromThisAppDomain;
239 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
241 public bool IsFromThisProcess ()
243 foreach (object data in channel_info.ChannelData)
245 if (data is CrossAppDomainData)
247 string refProcId = ((CrossAppDomainData)data).ProcessID;
248 return (refProcId == RemotingConfiguration.ProcessId);
255 internal void UpdateChannelInfo()
257 channel_info = new ChannelInfo ();
260 internal Type ServerType
264 if (_serverType == null) _serverType = Type.GetType (typeInfo.TypeName);