4 using System.Collections.Generic;
6 using System.Reflection;
8 namespace SiliconStudio.Core.Reflection
17 private readonly Dictionary<MemberInfoKey, IReadOnlyCollection<Attribute>> cachedAttributes =
new Dictionary<MemberInfoKey, IReadOnlyCollection<Attribute>>();
18 private readonly Dictionary<MemberInfo, List<Attribute>> registeredAttributes =
new Dictionary<MemberInfo, List<Attribute>>();
26 public virtual IReadOnlyCollection<Attribute>
GetAttributes(MemberInfo memberInfo,
bool inherit =
true)
28 var key =
new MemberInfoKey(memberInfo, inherit);
31 IReadOnlyCollection<Attribute> attributes;
32 lock (cachedAttributes)
34 if (cachedAttributes.TryGetValue(key, out attributes))
40 var defaultAttributes = memberInfo.GetCustomAttributes(inherit);
41 var attributesToCache = defaultAttributes.Cast<Attribute>().ToList();
44 List<Attribute> registered;
45 if (registeredAttributes.TryGetValue(memberInfo, out registered))
47 attributesToCache.AddRange(registered);
50 attributes = attributesToCache.AsReadOnly();
53 cachedAttributes.Add(key, attributes);
64 public void Register(MemberInfo memberInfo, Attribute attribute)
66 lock (cachedAttributes)
68 List<Attribute> attributes;
69 if (!registeredAttributes.TryGetValue(memberInfo, out attributes))
71 attributes =
new List<Attribute>();
72 registeredAttributes.Add(memberInfo, attributes);
75 attributes.Add(attribute);
79 private struct MemberInfoKey : IEquatable<MemberInfoKey>
81 private readonly MemberInfo memberInfo;
83 private readonly
bool inherit;
85 public MemberInfoKey(MemberInfo memberInfo,
bool inherit)
87 this.memberInfo = memberInfo;
88 this.inherit = inherit;
91 public bool Equals(MemberInfoKey other)
93 return memberInfo.Equals(other.memberInfo) && inherit.Equals(other.inherit);
96 public override bool Equals(
object obj)
98 if (ReferenceEquals(null, obj))
return false;
99 return obj is MemberInfoKey && Equals((MemberInfoKey) obj);
102 public override int GetHashCode()
106 return (memberInfo.GetHashCode()*397) ^ inherit.GetHashCode();
void Register(MemberInfo memberInfo, Attribute attribute)
Registers an attribute for the specified member. Restriction: Attributes registered this way cannot b...
A registry for all attributes.
virtual IReadOnlyCollection< Attribute > GetAttributes(MemberInfo memberInfo, bool inherit=true)
Gets the attributes associated with the specified member.
A default implementation for IAttributeRegistry. This implementation allows to retrieve default attri...