It's not a question of major importance, but I was wondering why the Thread class exposes a property for getting the current Context (Thread.CurrentContext) and a method for getting the current AppDomain (Thread.GetDomain()).
Knowing the hierarchy of Process > AppDomain > Context > Thread, my assumption would be that the context for thread is known at current point in time, and the domain needs to be searched based on current context.
But I'd like to hear wiser answers. Thanks!
Indeed, in the current implementation of the .NET Framework the
Context
object keeps a reference to its parent domain. The Framework designers might have exposed the context's domain asThread.Context.Domain
. It probably would be a rhetorical question why they didn't do so; I can't tell that by looking into the reference source code.What matters is that at any given moment of time, the thread is executing code inside a particular domain. This would be either the process's default domain, or the domain entered via
AppDomain.DoCallBack
,AppDomain.ExecuteAssembly
or a marshalledMarshalByRefObject
-object. That'd would be the domainThread.GetDomain()
returns.This domain has at least one context (the default one), but it may also have other contexts, created for
ContextBoundObject
-objects. It's possible to enter any of those contexts explicitly on the same domain viaContext.DoCallBack
or implicitly from any domain by calling a marshalledContextBoundObject
-object. That'd be the contextThread.Context
returns.There is no parent-child relationship between thread and domain or thread and context. However, there is strict parent-child, one-to-many relationship between domain and its contexts. So, the domain doesn't need to be searched based on current context.
If you like to play with it a bit more, here is the app I used: