3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** File: MarshalByRefObject.cs
12 ** Purpose: Defines the root type for all marshal by reference aka
13 ** AppDomain bound types
17 ===========================================================*/
21 using System.Security;
22 using System.Security.Permissions;
23 using System.Threading;
24 using System.Runtime.Remoting;
26 using System.Runtime.Remoting.Lifetime;
27 using System.Runtime.Remoting.Services;
29 using System.Runtime.InteropServices;
30 using System.Reflection;
31 using System.Runtime.CompilerServices;
32 using System.Runtime.Versioning;
33 using System.Diagnostics.Contracts;
34 using CultureInfo = System.Globalization.CultureInfo;
37 [System.Runtime.InteropServices.ComVisible(true)]
38 public abstract class MarshalByRefObject
41 private Object __identity;
43 private Object Identity { get { return __identity; } set { __identity = value; } }
44 #if FEATURE_COMINTEROP
45 [System.Security.SecuritySafeCritical] // auto-generated
46 internal IntPtr GetComIUnknown(bool fIsBeingMarshalled)
49 if(RemotingServices.IsTransparentProxy(this))
51 pUnk = RemotingServices.GetRealProxy(this).GetCOMIUnknown(fIsBeingMarshalled);
55 pUnk = Marshal.GetIUnknownForObject(this);
60 [System.Security.SecurityCritical] // auto-generated
61 [MethodImplAttribute(MethodImplOptions.InternalCall)]
62 [ResourceExposure(ResourceScope.None)]
63 internal static extern IntPtr GetComIUnknown(MarshalByRefObject o);
64 #endif // FEATURE_COMINTEROP
66 // (1) for remote COM objects IsInstance of can't be executed on
67 // the proxies, so we need this method to be executed on the
69 // (2) for remote objects that do not have the complete type information
70 // we intercept calls to check the type and execute it on the actual
72 internal bool IsInstanceOfType(Type T)
74 return T.IsInstanceOfType(this);
77 // for remote COM Objects the late binding methods can't be
78 // executed on proxies, so we need this method to execute on
80 internal Object InvokeMember(String name,BindingFlags invokeAttr,Binder binder,
81 Object[] args,ParameterModifier[] modifiers,CultureInfo culture,String[] namedParameters)
87 throw new InvalidOperationException(Environment.GetResourceString("Arg_InvokeMember"));
89 // Call into the runtime to invoke on the COM object.
90 return t.InvokeMember(name, invokeAttr, binder, this, args, modifiers, culture, namedParameters);
93 // Returns a new cloned MBR instance that is a memberwise copy of this
94 // with the identity nulled out, so there are no identity conflicts
95 // when the cloned object is marshalled
96 protected MarshalByRefObject MemberwiseClone(bool cloneIdentity)
98 MarshalByRefObject mbr = (MarshalByRefObject)base.MemberwiseClone();
99 // set the identity on the cloned object to null
107 // A helper routine to extract the identity either from the marshalbyrefobject base
108 // class if it is not a proxy, otherwise from the real proxy.
109 // A flag is set to indicate whether the object passed in is a server or a proxy
110 [System.Security.SecuritySafeCritical] // auto-generated
111 internal static Identity GetIdentity(MarshalByRefObject obj, out bool fServer)
118 if(!RemotingServices.IsTransparentProxy(obj))
120 id = (Identity)obj.Identity;
124 // Toggle flag to indicate that we have a proxy
126 id = RemotingServices.GetRealProxy(obj).IdentityObject;
133 // Another helper that delegates to the helper above
134 internal static Identity GetIdentity(MarshalByRefObject obj)
136 Contract.Assert(!RemotingServices.IsTransparentProxy(obj), "Use this method for server objects only");
139 return GetIdentity(obj, out fServer);
143 internal ServerIdentity __RaceSetServerIdentity(ServerIdentity id)
145 if (__identity == null)
147 // For strictly MBR types, the TP field in the identity
148 // holds the real server
149 if (!id.IsContextBound)
151 id.RaceSetTransparentProxy(this);
153 Interlocked.CompareExchange(ref __identity, id, null);
155 return (ServerIdentity)__identity;
159 internal void __ResetServerIdentity()
164 // This method is used return a lifetime service object which
165 // is used to control the lifetime policy to the object.
166 // For the default Lifetime service this will be an object of typoe ILease.
168 [System.Security.SecurityCritical] // auto-generated_required
169 public Object GetLifetimeService()
171 return LifetimeServices.GetLease(this);
174 // This method is used return lifetime service object. This method
175 // can be overridden to return a LifetimeService object with properties unique to
177 // For the default Lifetime service this will be an object of type ILease.
179 [System.Security.SecurityCritical] // auto-generated_required
180 public virtual Object InitializeLifetimeService()
182 return LifetimeServices.GetLeaseInitial(this);
185 [System.Security.SecurityCritical] // auto-generated_required
186 public virtual ObjRef CreateObjRef(Type requestedType)
188 if(__identity == null)
190 throw new RemotingException(Environment.GetResourceString(
191 "Remoting_NoIdentityEntry"));
193 return new ObjRef(this, requestedType);
196 // This is for casting interop ObjRefLite's.
197 // ObjRefLite's have been deprecated. These methods are not exposed
198 // through any user APIs and would be removed in the future
199 [System.Security.SecuritySafeCritical] // auto-generated
200 internal bool CanCastToXmlType(String xmlTypeName, String xmlTypeNamespace)
202 Type castType = SoapServices.GetInteropTypeFromXmlType(xmlTypeName, xmlTypeNamespace);
203 if (castType == null)
205 String typeNamespace;
207 if (!SoapServices.DecodeXmlNamespaceForClrTypeNamespace(xmlTypeNamespace,
208 out typeNamespace, out assemblyName))
212 if ((typeNamespace != null) && (typeNamespace.Length > 0))
213 typeName = typeNamespace + "." + xmlTypeName;
215 typeName = xmlTypeName;
219 Assembly asm = Assembly.Load(assemblyName);
220 castType = asm.GetType(typeName, false, false);
228 if (castType != null)
229 return castType.IsAssignableFrom(this.GetType());
232 } // CanCastToXmlType
234 // helper method for calling CanCastToXmlType
235 // ObjRefLite's have been deprecated. These methods are not exposed
236 // through any user APIs and would be removed in the future
237 [System.Security.SecuritySafeCritical] // auto-generated
238 internal static bool CanCastToXmlTypeHelper(RuntimeType castType, MarshalByRefObject o)
240 if (castType == null)
241 throw new ArgumentNullException("castType");
243 Contract.EndContractBlock();
244 // MarshalByRefObject's can only be casted to MarshalByRefObject's or interfaces.
245 if (!castType.IsInterface && !castType.IsMarshalByRef)
248 // figure out xml type name
249 String xmlTypeName = null;
250 String xmlTypeNamespace = null;
251 if (!SoapServices.GetXmlTypeForInteropType(castType, out xmlTypeName, out xmlTypeNamespace))
253 // There's no registered interop type name, so just use the default.
254 xmlTypeName = castType.Name;
256 SoapServices.CodeXmlNamespaceForClrTypeNamespace(
257 castType.Namespace, castType.GetRuntimeAssembly().GetSimpleName());
260 return o.CanCastToXmlType(xmlTypeName, xmlTypeNamespace);
261 } // CanCastToXmlType
263 #endif // FEATURE_REMOTING