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;
28 [NonSerialized] bool marshalledValue = true;
32 // no idea why this needs to be public
33 channel_info = new ChannelInfoStore ();
36 public ObjRef (MarshalByRefObject mbr, Type type)
39 throw new ArgumentNullException ("mbr");
42 throw new ArgumentNullException ("type");
44 // The ObjRef can only be constructed if the given mbr
45 // has already been marshalled using RemotingServices.Marshall
47 uri = RemotingServices.GetObjectUri(mbr);
48 typeInfo = new TypeInfo(type);
50 if (!typeInfo.CanCastTo(mbr.GetType(), mbr))
51 throw new RemotingException ("The server object type cannot be cast to the requested type " + type.FullName + ".");
53 channel_info = new ChannelInfoStore ();
56 protected ObjRef (SerializationInfo si, StreamingContext sc)
58 SerializationInfoEnumerator en = si.GetEnumerator();
59 // Info to serialize: uri, objrefFlags, typeInfo, envoyInfo, channelInfo
61 marshalledValue = true;
63 while (en.MoveNext ()) {
66 uri = (string)en.Value;
69 typeInfo = (IRemotingTypeInfo)en.Value;
72 channel_info = (IChannelInfo)en.Value;
75 envoyInfo = (IEnvoyInfo)en.Value;
80 if (o.GetType().Equals(typeof(String)))
\r
81 status = ((IConvertible) o).ToInt32(null);
\r
86 marshalledValue = false;
88 case "objrefFlags": // FIXME: do something with this
91 throw new NotSupportedException ();
96 public virtual IChannelInfo ChannelInfo {
103 channel_info = value;
108 public virtual IEnvoyInfo EnvoyInfo {
110 throw new NotImplementedException ();
113 throw new NotImplementedException ();
118 public virtual IRemotingTypeInfo TypeInfo {
127 public virtual string URI {
136 public virtual void GetObjectData (SerializationInfo si, StreamingContext sc)
138 si.SetType (GetType());
139 si.AddValue ("uri", uri);
140 si.AddValue ("typeInfo", typeInfo, typeof (IRemotingTypeInfo));
141 si.AddValue ("envoyInfo", envoyInfo, typeof (IEnvoyInfo));
142 si.AddValue ("channelInfo", channel_info, typeof(IChannelInfo));
143 si.AddValue ("objrefFlags", 0);
146 public virtual object GetRealObject (StreamingContext sc)
149 return RemotingServices.GetRemoteObject(Type.GetType(typeInfo.TypeName), null, channel_info.ChannelData, uri);
154 public bool IsFromThisAppDomain ()
156 Identity identity = RemotingServices.GetIdentityForUri (uri);
157 if (identity == null) return false; // URI not registered in this domain
159 return identity.IsFromThisAppDomain;
163 public bool IsFromThisProcess ()
165 // as yet we do not consider this optimization