Overview
Binding is the operation of relating an object residing inside
the Code Model representation of a specific .NET module to the
System.Reflection representation (Type,
MethodBase, FieldInfo, ...).
PostSharp offers two solutions to bind the Code Object Model
toSystem.Reflection: binding to system runtime objects
or wrappers. Conversely, you can bind any
System.Reflection representation to the Code Object
Model.
Binding to system runtime objects
System runtime objects are the internal representations that the
Virtual Runtime Engine maintains. When you use
System.Reflection (for instance when you get a type
with the typeof keyword), you get precisely these
representations. Not only you get access to metadata information,
but you are able to interact with runtime objects: you can invoke
methods, set field values and so on.
PostSharp currently implements binding of PostShart assemblies, types and methods to System.Reflection types and methods, and conversely.
Mapping from PostSharp to System.Reflection is provided by the
methods IAssembly.GetSystemAssembly,IType.GetSystemType
andIMethod.GetSystemMethod.
Ambiguous matches
System.Reflection is not a one-to-one
representation of .NET assemblies. It has been optimized for
usability in user code. The price to pay is many ambiguous matches
between System.Reflection and PostSharp:
-
System.Reflectiondoes not make a difference between intrinsic types (object,string,int32,int64, ...) and their equivalent of explicit types in the System namespace (System.Object,System.String,System.Int32,System.Int64, ...). Fortunately PostSharp implements heuristics that seem to perform well with code generated according to Microsoft recommendations (i.e. using intrinsic types whenever possible). - There may be confusion between
void*andSystem.IntPtr.
Reflection wrappers
Reflection wrappers literally wrap the PostSharp Code Object
Model into the System.Reflection objet model. It does
not rely on runtime system representations.
The benefit of wrappers is that you can still expose the
System.Reflection object model even when you cannot or
do now want to load the type or the assembly in the application
domain. This allows for instance to reflect types that contain
unresolved external methods.
Contrarly to system runtime objects of
System.Reflection, reflection wrappers give you only
access to metadata and do not allow to interact with runtime
objects. Reflection wrappers may also support a smaller set of
functionalities, so it is better (and any way more performant) to
rely on true system runtime objects where possible.
Binding from System.Reflection to PostSharp
Given an object of System.Reflection
(Type, MethodBase,
FieldInfo, ...), you can always get its representation
in PostSharp. Note that this representation will always depend on
the module.
This functionality is provided by methods ModuleDeclaration.FindAssembly,
ModuleDeclaration.FindType
and ModuleDeclaration.FindMethod.