2 using System
.Collections
;
3 using System
.Collections
.Generic
;
5 using System
.Reflection
;
6 using System
.Reflection
.Emit
;
7 using System
.Runtime
.Serialization
.Formatters
.Binary
;
8 using Castle
.DynamicProxy
.Generators
;
10 namespace Castle
.DynamicProxy
13 /// Applied to the assemblies saved by <see cref="ModuleScope"/> in order to persist the cache data included in the persisted assembly.
15 [AttributeUsage (AttributeTargets
.Assembly
, AllowMultiple
= false)]
16 [CLSCompliant (false)]
17 public class CacheMappingsAttribute
: Attribute
19 private static readonly ConstructorInfo constructor
= typeof (CacheMappingsAttribute
).GetConstructor (new Type
[] { typeof (byte[]) }
);
21 public static void ApplyTo (AssemblyBuilder assemblyBuilder
, Dictionary
<CacheKey
, string> mappings
)
23 using (MemoryStream stream
= new MemoryStream ())
25 BinaryFormatter formatter
= new BinaryFormatter();
26 formatter
.Serialize (stream
, mappings
);
27 byte[] bytes
= stream
.ToArray();
28 CustomAttributeBuilder attributeBuilder
= new CustomAttributeBuilder (constructor
, new object[] {bytes}
);
29 assemblyBuilder
.SetCustomAttribute (attributeBuilder
);
33 private readonly byte[] _serializedCacheMappings
;
35 public CacheMappingsAttribute (byte[] serializedCacheMappings
)
37 _serializedCacheMappings
= serializedCacheMappings
;
40 public byte[] SerializedCacheMappings
42 get { return _serializedCacheMappings; }
45 public Dictionary
<CacheKey
, string> GetDeserializedMappings ()
47 using (MemoryStream stream
= new MemoryStream (SerializedCacheMappings
))
49 BinaryFormatter formatter
= new BinaryFormatter ();
50 return (Dictionary
<CacheKey
, string>) formatter
.Deserialize (stream
);