CacheMappingsAttribute
Applied to the assemblies saved by ModuleScope in order to persist the cache data included in the persisted assembly.
using Castle.DynamicProxy.Generators;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Serialization.Formatters.Binary;
namespace Castle.DynamicProxy.Serialization
{
[CLSCompliant(false)]
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public class CacheMappingsAttribute : Attribute
{
private static readonly ConstructorInfo constructor = typeof(CacheMappingsAttribute).GetConstructor(new Type[1] {
typeof(byte[])
});
private readonly byte[] serializedCacheMappings;
public byte[] SerializedCacheMappings => serializedCacheMappings;
public CacheMappingsAttribute(byte[] serializedCacheMappings)
{
this.serializedCacheMappings = serializedCacheMappings;
}
public Dictionary<CacheKey, string> GetDeserializedMappings()
{
using (MemoryStream serializationStream = new MemoryStream(SerializedCacheMappings)) {
BinaryFormatter binaryFormatter = new BinaryFormatter();
return (Dictionary<CacheKey, string>)binaryFormatter.Deserialize(serializationStream);
}
}
public static void ApplyTo(AssemblyBuilder assemblyBuilder, Dictionary<CacheKey, string> mappings)
{
using (MemoryStream memoryStream = new MemoryStream()) {
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, mappings);
byte[] array = memoryStream.ToArray();
CustomAttributeBuilder customAttribute = new CustomAttributeBuilder(constructor, new object[1] {
array
});
assemblyBuilder.SetCustomAttribute(customAttribute);
}
}
}
}