private Dictionary<TypeDefinition, List<CustomAttribute>> ExtractWeaveAttributesByTargetTypes()
{
Contract.Ensures(Contract.Result<Dictionary<TypeDefinition, List<CustomAttribute>>>().Keys.All(type => type != null));
Contract.Ensures(!Contract.Result<Dictionary<TypeDefinition, List<CustomAttribute>>>().Values.Any(
customAttributes => customAttributes == null || customAttributes.Any(customAttribute => customAttribute == null)));
var weaveAttributeInterfaceType = this.ModuleDefinition.Import(typeof(IWeaveAttribute)).Resolve();
var weaveAttributesByTargetTypes = new Dictionary<TypeDefinition, List<CustomAttribute>>();
foreach (var type in this.ModuleDefinition.Types)
{
Contract.Assert(type != null);
Contract.Assert(type.CustomAttributes != null);
for (var i = type.CustomAttributes.Count - 1; i >= 0; i--)
{
var attribute = type.CustomAttributes[i];
Contract.Assert(attribute != null);
var attributeTypeDefinition = attribute.AttributeType.Resolve();
Contract.Assert(attributeTypeDefinition != null);
foreach (var attributeInterfaceType in attributeTypeDefinition.Interfaces)
{
if (attributeInterfaceType.Resolve() == weaveAttributeInterfaceType)
{
List<CustomAttribute> weaveAttributesForType;
if (!weaveAttributesByTargetTypes.TryGetValue(type.Resolve(), out weaveAttributesForType))
{
weaveAttributesForType = new List<CustomAttribute>();
weaveAttributesByTargetTypes[type.Resolve()] = weaveAttributesForType;
}
weaveAttributesForType.Add(attribute);
type.CustomAttributes.RemoveAt(i);
}
}
}
}
return weaveAttributesByTargetTypes;
}