Embedding resources in .NET allows for data to be packaged together with your code in an assembly. Resources are normally specified at design time and then embedded by the compiler during build time.
PostSharp's AssemblyLevelAspect adds additional flexibility by allowing you to programmatically add resources at compile time. In doing so, you can add logic, and therefore flexibility, in determining which resources are embedded and how. For example, you could use this feature to encrypt a resource just before embedding it into your assembly.
Introducing resources
In the following example, we create an assembly-level aspect that builds a string during compilation and stores it in the current assembly as a resource. The example then shows that this string can be retrieved from the assembly at run time.
Create a class called
AddBuildInfoAspectthat derives from AssemblyLevelAspect. Also implement IAspectProvider, which exposes the ProvideAspects(object) method. The ProvideAspects(object) method will be called once by PostSharp, providing access to assembly information and allowing for a resource to be programmatically added to the assembly:[PSerializable] public sealed class AddBuildInfoAspect : AssemblyLevelAspect, IAspectProvider { public IEnumerable<AspectInstance> ProvideAspects( object targetElement ) { Assembly assembly = (Assembly) targetElement; byte[] buildInfoData = Encoding.ASCII.GetBytes( assembly.GetName().Name + " was enhanced by AddBuildInfoAspect." ); ManagedResourceIntroductionAspect resourceAspect = new ManagedResourceIntroductionAspect( "BuildInfo", buildInfoData ); yield return new AspectInstance( assembly, resourceAspect ); } }In this example, the
targetElementobject passed in is cast to anAssemblyobject from which the assembly name is retrieved. The code then builds a string that contains the assembly name, and converts this string to a byte array. A real aspect could store other information that is only known at build time, for instance the build date or the name of the build machine. The byte array is then stored along with a name for the data in PostSharp’s ManagedResourceIntroductionAspect object, and returned via an AspectInstance. PostSharp then embeds the resource into the current assembly.Add the
AddBuildInfoAspectattribute to the assembly, for instance in the AssemblyInfo.cs file:[assembly: AddBuildInfoAspect]
With this code in place, the assembly embeds the string as a resource into itself during compilation.
The following code demonstrates how to retrieve the data at run time:
class Program
{
static void Main()
{
Assembly assembly = Assembly.GetExecutingAssembly();
using Stream stream = assembly.GetManifestResourceStream( "BuildInfo" )!;
using StreamReader reader = new StreamReader( stream, Encoding.ASCII );
Console.WriteLine( reader.ReadToEnd() );
}
}
This will display the following line in the console window:
ResourceIntroduction was enhanced by AddBuildInfoAspect.
See Also
Reference
AssemblyLevelAspect
IAspectProvider
ManagedResourceIntroductionAspect
AspectInstance
Other Resources