CHAPTER 17 PROCESSES, APPDOMAINS, AND OBJECT CONTEXTS
{
}
// Get context information and print out context ID.
Context ctx = Thread.CurrentContext;
Console.WriteLine("{0} object in context {1}",
this.ToString(), ctx.ContextID);
foreach(IContextProperty itfCtxProp in ctx.ContextProperties)
Console.WriteLine("-> Ctx Prop: {0}", itfCtxProp.Name);
}
// SportsCarTS demands to be loaded in
// a synchronization context.
[Synchronization]
class SportsCarTS : ContextBoundObject
{
public SportsCarTS()
{
// Get context information and print out context ID.
Context ctx = Thread.CurrentContext;
Console.WriteLine("{0} object in context {1}",
this.ToString(), ctx.ContextID);
foreach(IContextProperty itfCtxProp in ctx.ContextProperties)
Console.WriteLine("-> Ctx Prop: {0}", itfCtxProp.Name);
}
}
Notice that each constructor obtains a Context object from the current thread of execution, via the
static Thread.CurrentContext property. Using the Context object, you are able to print out statistics
about the contextual boundary, such as its assigned ID, as well as a set of descriptors obtained via
Context.ContextProperties. This property returns an array of objects implementing the
IContextProperty interface, which exposes each descriptor through the Name property. Now, update
Main() to allocate an instance of each class type, like so:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Object Context *****\n");
// Objects will display contextual info upon creation.
SportsCar sport = new SportsCar();
Console.WriteLine();
SportsCar sport2 = new SportsCar();
Console.WriteLine();
}
SportsCarTS synchroSport = new SportsCarTS();
Console.ReadLine