Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AttributeRegistry.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Reflection;
7 
8 namespace SiliconStudio.Core.Reflection
9 {
10  /// <summary>
11  /// A default implementation for <see cref="IAttributeRegistry"/>.
12  /// This implementation allows to retrieve default attributes for a member or
13  /// to attach an attribute to a specific type/member.
14  /// </summary>
16  {
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>>();
19 
20  /// <summary>
21  /// Gets the attributes associated with the specified member.
22  /// </summary>
23  /// <param name="memberInfo">The reflection member.</param>
24  /// <param name="inherit">if set to <c>true</c> includes inherited attributes.</param>
25  /// <returns>An enumeration of <see cref="Attribute"/>.</returns>
26  public virtual IReadOnlyCollection<Attribute> GetAttributes(MemberInfo memberInfo, bool inherit = true)
27  {
28  var key = new MemberInfoKey(memberInfo, inherit);
29 
30  // Use a cache of attributes
31  IReadOnlyCollection<Attribute> attributes;
32  lock (cachedAttributes)
33  {
34  if (cachedAttributes.TryGetValue(key, out attributes))
35  {
36  return attributes;
37  }
38 
39  // Else retrieve all default attributes
40  var defaultAttributes = memberInfo.GetCustomAttributes(inherit);
41  var attributesToCache = defaultAttributes.Cast<Attribute>().ToList();
42 
43  // And add registered attributes
44  List<Attribute> registered;
45  if (registeredAttributes.TryGetValue(memberInfo, out registered))
46  {
47  attributesToCache.AddRange(registered);
48  }
49 
50  attributes = attributesToCache.AsReadOnly();
51 
52  // Add to the cache
53  cachedAttributes.Add(key, attributes);
54  }
55 
56  return attributes;
57  }
58 
59  /// <summary>
60  /// Registers an attribute for the specified member. Restriction: Attributes registered this way cannot be listed in inherited attributes.
61  /// </summary>
62  /// <param name="memberInfo">The member information.</param>
63  /// <param name="attribute">The attribute.</param>
64  public void Register(MemberInfo memberInfo, Attribute attribute)
65  {
66  lock (cachedAttributes)
67  {
68  List<Attribute> attributes;
69  if (!registeredAttributes.TryGetValue(memberInfo, out attributes))
70  {
71  attributes = new List<Attribute>();
72  registeredAttributes.Add(memberInfo, attributes);
73  }
74 
75  attributes.Add(attribute);
76  }
77  }
78 
79  private struct MemberInfoKey : IEquatable<MemberInfoKey>
80  {
81  private readonly MemberInfo memberInfo;
82 
83  private readonly bool inherit;
84 
85  public MemberInfoKey(MemberInfo memberInfo, bool inherit)
86  {
87  this.memberInfo = memberInfo;
88  this.inherit = inherit;
89  }
90 
91  public bool Equals(MemberInfoKey other)
92  {
93  return memberInfo.Equals(other.memberInfo) && inherit.Equals(other.inherit);
94  }
95 
96  public override bool Equals(object obj)
97  {
98  if (ReferenceEquals(null, obj)) return false;
99  return obj is MemberInfoKey && Equals((MemberInfoKey) obj);
100  }
101 
102  public override int GetHashCode()
103  {
104  unchecked
105  {
106  return (memberInfo.GetHashCode()*397) ^ inherit.GetHashCode();
107  }
108  }
109  }
110  }
111 }
void Register(MemberInfo memberInfo, Attribute attribute)
Registers an attribute for the specified member. Restriction: Attributes registered this way cannot b...
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...