3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** File: ContextProperty.cs
10 ** A contextProperty is a name-value pair holding the property
11 ** name and the object representing the property in a context.
12 ** An array of these is returned by Context::GetContextProperties()
16 ===========================================================*/
18 namespace System.Runtime.Remoting.Contexts {
21 using System.Threading;
22 using System.Reflection;
23 using System.Runtime.InteropServices;
24 using System.Runtime.CompilerServices;
25 using System.Runtime.Remoting.Activation;
26 using System.Security.Permissions;
27 using System.Diagnostics.Contracts;
30 [System.Security.SecurityCritical] // auto-generated_required
31 [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.Infrastructure)]
32 [System.Runtime.InteropServices.ComVisible(true)]
33 public class ContextProperty {
34 internal String _name; // property name
35 internal Object _property; // property object
38 public virtual String Name {
45 public virtual Object Property {
51 /* can't create outside the package */
52 internal ContextProperty(String name, Object prop)
59 // The IContextAttribute interface is implemented by attribute classes.
60 // The attributes contribute a property which resides in a context and
61 // enforces a specific policy for the objects created in that context.
63 [System.Runtime.InteropServices.ComVisible(true)]
64 public interface IContextAttribute
67 [System.Security.SecurityCritical] // auto-generated_required
68 bool IsContextOK(Context ctx, IConstructionCallMessage msg);
70 [System.Security.SecurityCritical] // auto-generated_required
71 void GetPropertiesForNewContext(IConstructionCallMessage msg);
74 // This interface is exposed by the property contributed to a context
75 // by an attribute. By default, it is also implemented by the ContextAttribute
76 // base class which every attribute class must extend from.
78 [System.Runtime.InteropServices.ComVisible(true)]
79 public interface IContextProperty
82 // This is the name under which the property will be added
83 // to the {name,property} table in a context.
86 [System.Security.SecurityCritical] // auto-generated_required
90 // After forming the newCtx, we ask each property if it is happy
91 // with the context. We expect most implementations to say yes.
92 [System.Security.SecurityCritical] // auto-generated_required
93 bool IsNewContextOK(Context newCtx);
96 // New method. All properties are notified when the context
97 // they are in is frozen.
98 [System.Security.SecurityCritical] // auto-generated_required
99 void Freeze(Context newContext);
103 [System.Runtime.InteropServices.ComVisible(true)]
104 public interface IContextPropertyActivator
107 // This method lets properties in the current context have a say in
108 // whether an activation may be done 'here' or not.
109 [System.Security.SecurityCritical] // auto-generated_required
110 bool IsOKToActivate(IConstructionCallMessage msg);
112 [System.Security.SecurityCritical] // auto-generated_required
113 void CollectFromClientContext(IConstructionCallMessage msg);
115 [System.Security.SecurityCritical] // auto-generated_required
116 bool DeliverClientContextToServerContext(IConstructionCallMessage msg);
118 [System.Security.SecurityCritical] // auto-generated_required
119 void CollectFromServerContext(IConstructionReturnMessage msg);
121 [System.Security.SecurityCritical] // auto-generated_required
122 bool DeliverServerContextToClientContext(IConstructionReturnMessage msg);
126 // All context attribute classes must extend from this base class.
127 // This class provides the base implementations which the derived
128 // classes are free to over-ride. The base implementations provide
129 // the default answers to various questions.
132 [System.Security.SecurityCritical] // auto-generated_required
134 [AttributeUsage(AttributeTargets.Class)]
135 [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.Infrastructure)]
136 [System.Runtime.InteropServices.ComVisible(true)]
137 public class ContextAttribute
138 : Attribute, IContextAttribute, IContextProperty
141 protected String AttributeName;
143 // The derived class must call: base(name);
145 public ContextAttribute(String name)
147 AttributeName = name;
150 // IContextPropery::Name
151 // Default implementation provides AttributeName as the property name.
153 public virtual String Name
155 [System.Security.SecurityCritical]
156 get { return AttributeName; }
159 // IContextProperty::IsNewContextOK
161 [System.Security.SecurityCritical]
162 public virtual bool IsNewContextOK(Context newCtx)
164 // This will be called before entering the newCtx
165 // Default implementation says OK.
169 // IContextProperty::Freeze
170 // Default implementation does nothing
172 [System.Security.SecurityCritical]
173 public virtual void Freeze(Context newContext)
175 BCLDebug.Log("ContextAttribute::ContextProperty::Freeze"+
176 " for context " + newContext );
180 // Default implementation just compares the names
182 [System.Security.SecuritySafeCritical] // overrides public transparent method
183 public override bool Equals(Object o)
185 IContextProperty prop = o as IContextProperty;
186 return (null != prop) && AttributeName.Equals(prop.Name);
190 [System.Security.SecuritySafeCritical] // overrides public transparent method
191 public override int GetHashCode()
193 return this.AttributeName.GetHashCode();
196 // IContextAttribute::IsContextOK
197 // Default calls Object::Equals on the property and does not
198 // bother with the ctorMsg.
200 [System.Security.SecurityCritical] // auto-generated
201 public virtual bool IsContextOK(
202 Context ctx, IConstructionCallMessage ctorMsg)
205 throw new ArgumentNullException("ctx");
207 throw new ArgumentNullException("ctorMsg");
208 Contract.EndContractBlock();
210 Contract.Assert(ctorMsg.ActivationType.IsMarshalByRef, "Activation on a non MarshalByRef object");
212 if (!ctorMsg.ActivationType.IsContextful)
217 Object prop = ctx.GetProperty(AttributeName);
218 if ((prop!=null) && (Equals(prop)))
225 // IContextAttribute::GetPropertiesForNewContext
226 // Default adds the attribute itself w/o regard to the current
227 // list of properties
229 [System.Security.SecurityCritical] // auto-generated
230 public virtual void GetPropertiesForNewContext(
231 IConstructionCallMessage ctorMsg)
234 throw new ArgumentNullException("ctorMsg");
235 Contract.EndContractBlock();
236 ctorMsg.ContextProperties.Add((IContextProperty)this);
240 #if SIMPLEXAACTIVATION
242 [AttributeUsage(AttributeTargets.Class)]
243 [System.Runtime.InteropServices.ComVisible(true)]
244 public class new_appdomain : ContextAttribute
247 internal static int _domain_no = 0;
250 public new_appdomain() : base("new_appdomain") {}
253 [System.Runtime.InteropServices.ComVisible(true)]
254 public override bool IsContextOK(Context ctx, IConstructionCallMessage msg)
260 [System.Runtime.InteropServices.ComVisible(true)]
261 public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
263 ctorMsg.GetProperties()["__new_appdomain"] = true;
267 [System.Runtime.InteropServices.ComVisible(true)]
268 public static IConstructionReturnMessage DoSimpleXADActivation(IConstructionCallMessage msg)
270 int domain_no = Interlocked.Increment(ref _domain_no);
271 AppDomain ad = AppDomain.CreateDomain("AutoDomain #" + domain_no, null, null);
273 activator a = (activator) (ad.CreateInstance(null, typeof(activator).FullName)).Unwrap();
275 return a.Activate(msg);
279 [System.Runtime.InteropServices.ComVisible(true)]
280 public class activator : MarshalByRefObject, IActivator
283 [System.Runtime.InteropServices.ComVisible(true)]
284 public IConstructionReturnMessage Activate(IConstructionCallMessage msg)
286 return RemotingServices.DoCrossContextActivation(msg);
290 [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
291 public void LoadAssembly(AssemblyName an)
293 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
294 Assembly a = Assembly.InternalLoad(an, false, null, ref stackMark);
297 throw new RemotingException(
299 Environment.GetResourceString(
300 "Remoting_AssemblyLoadFailed"),
309 [System.Runtime.InteropServices.ComVisible(true)]
310 public class MBR : MarshalByRefObject
313 public String MyAppDomain()
315 return Thread.GetDomain().GetFriendlyName();
321 [System.Runtime.InteropServices.ComVisible(true)]
322 public class CB : ContextBoundObject
325 public String MyAppDomain()
327 return Thread.GetDomain().GetFriendlyName();