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 using System.Runtime.ConstrainedExecution;
44 namespace System.Runtime.Remoting {
47 [System.Runtime.InteropServices.ComVisible (true)]
48 public class ObjRef : IObjectReference, ISerializable
50 IChannelInfo channel_info;
52 IRemotingTypeInfo typeInfo;
57 static int MarshalledObjectRef = 1;
58 static int WellKnowObjectRef = 2;
62 // no idea why this needs to be public
67 internal ObjRef (string typeName, string uri, IChannelInfo cinfo)
71 typeInfo = new TypeInfo (Type.GetType (typeName, true));
74 internal ObjRef (ObjRef o, bool unmarshalAsProxy) {
75 channel_info = o.channel_info;
78 typeInfo = o.typeInfo;
79 envoyInfo = o.envoyInfo;
81 if (unmarshalAsProxy) flags |= MarshalledObjectRef;
84 public ObjRef (MarshalByRefObject o, Type requestedType)
87 throw new ArgumentNullException ("o");
89 if (requestedType == null)
90 throw new ArgumentNullException ("requestedType");
92 // The ObjRef can only be constructed if the given o
93 // has already been marshalled using RemotingServices.Marshall
95 uri = RemotingServices.GetObjectUri (o);
96 typeInfo = new TypeInfo (requestedType);
98 if (!requestedType.IsInstanceOfType (o))
99 throw new RemotingException ("The server object type cannot be cast to the requested type " + requestedType.FullName);
104 internal ObjRef (Type type, string url, object remoteChannelData)
107 typeInfo = new TypeInfo(type);
109 if (remoteChannelData != null)
110 channel_info = new ChannelInfo (remoteChannelData);
112 flags |= WellKnowObjectRef;
115 protected ObjRef (SerializationInfo info, StreamingContext context)
117 SerializationInfoEnumerator en = info.GetEnumerator();
118 // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
120 bool marshalledValue = true;
122 while (en.MoveNext ()) {
125 uri = (string)en.Value;
128 typeInfo = (IRemotingTypeInfo)en.Value;
131 channel_info = (IChannelInfo)en.Value;
134 envoyInfo = (IEnvoyInfo)en.Value;
136 case "fIsMarshalled":
140 status = ((IConvertible) o).ToInt32(null);
145 marshalledValue = false;
148 flags = Convert.ToInt32 (en.Value);
151 throw new NotSupportedException ();
154 if (marshalledValue) flags |= MarshalledObjectRef;
157 internal bool IsPossibleToCAD ()
159 // we should check if this obj ref belongs to a cross app context.
161 // Return false. If not, serialization of this ObjRef will not work
166 internal bool IsReferenceToWellKnow
168 get { return (flags & WellKnowObjectRef) > 0; }
171 public virtual IChannelInfo ChannelInfo {
172 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
178 channel_info = value;
182 public virtual IEnvoyInfo EnvoyInfo {
191 public virtual IRemotingTypeInfo TypeInfo {
200 public virtual string URI {
209 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
211 info.SetType (GetType());
212 info.AddValue ("uri", uri);
213 info.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
214 info.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
215 info.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
216 info.AddValue ("objrefFlags", flags);
219 public virtual object GetRealObject (StreamingContext context)
221 if ((flags & MarshalledObjectRef) > 0)
222 return RemotingServices.Unmarshal (this);
227 public bool IsFromThisAppDomain ()
229 Identity identity = RemotingServices.GetIdentityForUri (uri);
230 if (identity == null) return false; // URI not registered in this domain
232 return identity.IsFromThisAppDomain;
235 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
236 public bool IsFromThisProcess ()
238 foreach (object data in channel_info.ChannelData)
240 if (data is CrossAppDomainData)
242 string refProcId = ((CrossAppDomainData)data).ProcessID;
243 return (refProcId == RemotingConfiguration.ProcessId);
250 internal void UpdateChannelInfo()
252 channel_info = new ChannelInfo ();
255 internal Type ServerType
259 if (_serverType == null) _serverType = Type.GetType (typeInfo.TypeName);
264 internal void SetDomainID (int id)