Difference between the PostSharp Code Object Model and System.Reflection
PostSharp uses the entities defined in the specifications of the
Common Language Infrastructure (CLI). However, developers are more
familiar with the concepts of System.Reflection. They
should be aware than System.Reflection includes great
simplifications with respect to CLI specifications. CLI is module
oriented. A module is an atomic artifact that can bind to other
modules to form complete applications. In order to refer to types
defined outside the current module, one uses
references. By contrast, System.Reflection
lives in the application universe where all references
between modules are already resolved. In CLI and in PostSharp,
references are explicitly represented as separate entities. But in
System.Reflection, there is simply no concept of
reference.
Modules
Modules (ModuleDeclaration) are atomic artifacts (i.e. they cannot be separated in smaller units). .NET modules are in fact Portable Executables (PE) just like native libraries and executables.
Assemblies
There is a common confusion about assemblies. An assembly, as indicated by its name, is a set of assembled modules and other files. So an assembly can be composed of many modules and even of unmanaged modules! It is a common mistake and misleading to consider that an assembly is composed of a single file.
What defines intrinsically the assembly is its assembly manifest (AssemblyManifestDeclaration). The manifest is composed of a list of references to modules and external assemblies, and of a list of exported types. The assembly manifest is contained in 'principal' managed module of the assembly (and not in a separate XML file, as claims another frequent mistake).
So the assembly manifest (AssemblyManifestDeclaration) is contained in a module (ModuleDeclaration); it contains references to external modules (ModuleRefDeclaration) and external assemblies (AssemblyRefDeclaration).
So let's summarize:
- Modules, not assemblies, are the real atoms.
- Assemblies are composed of many modules (managed or unmanaged executables).
- Assemblies are defined by an assembly manifest.
- The assembly manifest is stored in a managed module.
- A managed module may, but is not required to, contain an assembly manifest.
PostSharp makes a difference between an assembly and an assembly manifest. The AssemblyEnvelope class encapsulates the assembly understood as a collection of modules. The AssemblyManifestDeclaration class encapsulates the manifest; it does not contain modules but is contained by a module. Where AssemblyManifestDeclaration contains a collection of references to modules, AssemblyEnvelope contain modules in themselves.
Domains
PostSharp domains (Domain) are similar to the .NET
class AppDomain. Domains are contexts in which
references between assemblies are solved. Just as in .NET,
PostSharp assemblies are always loaded in a domain. PostSharp
allows many domains to be loaded paralelly, even in the same
AppDomain.
The base of the object tree is the ModuleDeclaration type. Since PostSharp is module-oriented, everything is contained in the module even the assembly manifest.