|
1 | 1 | // Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
|
2 | 2 | // Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
|
3 |
| -using System; |
4 |
| -using System.Collections.Generic; |
5 |
| -using System.Linq; |
| 3 | + |
6 | 4 | using System.Reflection;
|
7 |
| -using Stride.Core.Annotations; |
8 | 5 |
|
9 |
| -namespace Stride.Core.Reflection |
| 6 | +namespace Stride.Core.Reflection; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A default implementation for <see cref="IAttributeRegistry"/>. |
| 10 | +/// This implementation allows to retrieve default attributes for a member or |
| 11 | +/// to attach an attribute to a specific type/member. |
| 12 | +/// </summary> |
| 13 | +public class AttributeRegistry : IAttributeRegistry |
10 | 14 | {
|
| 15 | + private readonly object lockObject = new(); |
| 16 | + private readonly Dictionary<MemberInfoKey, List<Attribute>> cachedAttributes = []; |
| 17 | + private readonly Dictionary<MemberInfo, List<Attribute>> registeredAttributes = []; |
| 18 | + |
| 19 | + // TODO: move this in a different location |
| 20 | + public Action<ObjectDescriptor, List<IMemberDescriptor>>? PrepareMembersCallback { get; set; } |
| 21 | + |
11 | 22 | /// <summary>
|
12 |
| - /// A default implementation for <see cref="IAttributeRegistry"/>. |
13 |
| - /// This implementation allows to retrieve default attributes for a member or |
14 |
| - /// to attach an attribute to a specific type/member. |
| 23 | + /// Gets the attributes associated with the specified member. |
15 | 24 | /// </summary>
|
16 |
| - public class AttributeRegistry : IAttributeRegistry |
| 25 | + /// <param name="memberInfo">The reflection member.</param> |
| 26 | + /// <param name="inherit">if set to <c>true</c> includes inherited attributes.</param> |
| 27 | + /// <returns>An enumeration of <see cref="Attribute"/>.</returns> |
| 28 | + public virtual List<Attribute> GetAttributes(MemberInfo memberInfo, bool inherit = true) |
17 | 29 | {
|
18 |
| - private readonly object lockObject = new(); |
19 |
| - private readonly Dictionary<MemberInfoKey, List<Attribute>> cachedAttributes = []; |
20 |
| - private readonly Dictionary<MemberInfo, List<Attribute>> registeredAttributes = []; |
21 |
| - |
22 |
| - // TODO: move this in a different location |
23 |
| - public Action<ObjectDescriptor, List<IMemberDescriptor>>? PrepareMembersCallback { get; set; } |
24 |
| - |
25 |
| - /// <summary> |
26 |
| - /// Gets the attributes associated with the specified member. |
27 |
| - /// </summary> |
28 |
| - /// <param name="memberInfo">The reflection member.</param> |
29 |
| - /// <param name="inherit">if set to <c>true</c> includes inherited attributes.</param> |
30 |
| - /// <returns>An enumeration of <see cref="Attribute"/>.</returns> |
31 |
| - public virtual List<Attribute> GetAttributes(MemberInfo memberInfo, bool inherit = true) |
| 30 | + var key = new MemberInfoKey(memberInfo, inherit); |
| 31 | + |
| 32 | + // Use a cache of attributes |
| 33 | + List<Attribute> attributes; |
| 34 | + lock (lockObject) |
32 | 35 | {
|
33 |
| - var key = new MemberInfoKey(memberInfo, inherit); |
| 36 | + if (cachedAttributes.TryGetValue(key, out var cacheAttributes)) |
| 37 | + { |
| 38 | + return cacheAttributes; |
| 39 | + } |
34 | 40 |
|
35 |
| - // Use a cache of attributes |
36 |
| - List<Attribute> attributes; |
37 |
| - lock (lockObject) |
| 41 | + // Else retrieve all default attributes |
| 42 | + var defaultAttributes = Attribute.GetCustomAttributes(memberInfo, inherit); |
| 43 | + IEnumerable<Attribute> attributesToCache = defaultAttributes; |
| 44 | + |
| 45 | + // And add registered attributes |
| 46 | + if (registeredAttributes.TryGetValue(memberInfo, out var registered)) |
38 | 47 | {
|
39 |
| - if (cachedAttributes.TryGetValue(key, out var cacheAttributes)) |
40 |
| - { |
41 |
| - return cacheAttributes; |
42 |
| - } |
43 |
| - |
44 |
| - // Else retrieve all default attributes |
45 |
| - var defaultAttributes = Attribute.GetCustomAttributes(memberInfo, inherit); |
46 |
| - IEnumerable<Attribute> attributesToCache = defaultAttributes; |
47 |
| - |
48 |
| - // And add registered attributes |
49 |
| - if (registeredAttributes.TryGetValue(memberInfo, out var registered)) |
50 |
| - { |
51 |
| - // Remove "real" attributes overridden by manually registered attributes |
52 |
| - attributesToCache = registered.Concat(defaultAttributes.Where(x => GetUsage(x).AllowMultiple || registered.All(y => y.GetType() != x.GetType()))); |
53 |
| - } |
54 |
| - |
55 |
| - attributes = attributesToCache.ToList(); |
56 |
| - |
57 |
| - // Add to the cache |
58 |
| - cachedAttributes.Add(key, attributes); |
| 48 | + // Remove "real" attributes overridden by manually registered attributes |
| 49 | + attributesToCache = registered.Concat(defaultAttributes.Where(x => GetUsage(x)!.AllowMultiple || registered.All(y => y.GetType() != x.GetType()))); |
59 | 50 | }
|
60 | 51 |
|
61 |
| - return attributes; |
| 52 | + attributes = attributesToCache.ToList(); |
| 53 | + |
| 54 | + // Add to the cache |
| 55 | + cachedAttributes.Add(key, attributes); |
62 | 56 | }
|
63 | 57 |
|
64 |
| - /// <summary> |
65 |
| - /// Registers an attribute for the specified member. Restriction: Attributes registered this way cannot be listed in inherited attributes. |
66 |
| - /// </summary> |
67 |
| - /// <param name="memberInfo">The member information.</param> |
68 |
| - /// <param name="attribute">The attribute.</param> |
69 |
| - public void Register(MemberInfo memberInfo, Attribute attribute) |
| 58 | + return attributes; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Registers an attribute for the specified member. Restriction: Attributes registered this way cannot be listed in inherited attributes. |
| 63 | + /// </summary> |
| 64 | + /// <param name="memberInfo">The member information.</param> |
| 65 | + /// <param name="attribute">The attribute.</param> |
| 66 | + public void Register(MemberInfo memberInfo, Attribute attribute) |
| 67 | + { |
| 68 | + lock (lockObject) |
70 | 69 | {
|
71 |
| - lock (lockObject) |
| 70 | + if (!registeredAttributes.TryGetValue(memberInfo, out var attributes)) |
72 | 71 | {
|
73 |
| - if (!registeredAttributes.TryGetValue(memberInfo, out var attributes)) |
74 |
| - { |
75 |
| - attributes = []; |
76 |
| - registeredAttributes.Add(memberInfo, attributes); |
77 |
| - } |
78 |
| - // Insert it in the first position to ensure it will override same attributes from base classes when using First |
79 |
| - attributes.Insert(0, attribute); |
80 |
| - |
81 |
| - cachedAttributes.Remove(new MemberInfoKey(memberInfo, true)); |
82 |
| - cachedAttributes.Remove(new MemberInfoKey(memberInfo, false)); |
| 72 | + attributes = []; |
| 73 | + registeredAttributes.Add(memberInfo, attributes); |
83 | 74 | }
|
84 |
| - } |
| 75 | + // Insert it in the first position to ensure it will override same attributes from base classes when using First |
| 76 | + attributes.Insert(0, attribute); |
85 | 77 |
|
86 |
| - private static AttributeUsageAttribute? GetUsage(Attribute attribute) |
87 |
| - { |
88 |
| - return Attribute.GetCustomAttribute(attribute.GetType(), typeof(AttributeUsageAttribute)) as AttributeUsageAttribute; |
| 78 | + cachedAttributes.Remove(new MemberInfoKey(memberInfo, true)); |
| 79 | + cachedAttributes.Remove(new MemberInfoKey(memberInfo, false)); |
89 | 80 | }
|
| 81 | + } |
90 | 82 |
|
91 |
| - private readonly struct MemberInfoKey : IEquatable<MemberInfoKey> |
92 |
| - { |
93 |
| - private readonly MemberInfo memberInfo; |
| 83 | + private static AttributeUsageAttribute? GetUsage(Attribute attribute) |
| 84 | + { |
| 85 | + return Attribute.GetCustomAttribute(attribute.GetType(), typeof(AttributeUsageAttribute)) as AttributeUsageAttribute; |
| 86 | + } |
94 | 87 |
|
95 |
| - private readonly bool inherit; |
| 88 | + private readonly struct MemberInfoKey : IEquatable<MemberInfoKey> |
| 89 | + { |
| 90 | + private readonly MemberInfo memberInfo; |
96 | 91 |
|
97 |
| - public MemberInfoKey(MemberInfo memberInfo, bool inherit) |
98 |
| - { |
99 |
| - ArgumentNullException.ThrowIfNull(memberInfo); |
100 |
| - this.memberInfo = memberInfo; |
101 |
| - this.inherit = inherit; |
102 |
| - } |
| 92 | + private readonly bool inherit; |
103 | 93 |
|
104 |
| - public bool Equals(MemberInfoKey other) |
105 |
| - { |
106 |
| - return memberInfo.Equals(other.memberInfo) && inherit.Equals(other.inherit); |
107 |
| - } |
| 94 | + public MemberInfoKey(MemberInfo memberInfo, bool inherit) |
| 95 | + { |
| 96 | + ArgumentNullException.ThrowIfNull(memberInfo); |
| 97 | + this.memberInfo = memberInfo; |
| 98 | + this.inherit = inherit; |
| 99 | + } |
108 | 100 |
|
109 |
| - public override bool Equals(object? obj) |
110 |
| - { |
111 |
| - if (ReferenceEquals(null, obj)) return false; |
112 |
| - return obj is MemberInfoKey key && Equals(key); |
113 |
| - } |
| 101 | + public bool Equals(MemberInfoKey other) |
| 102 | + { |
| 103 | + return memberInfo.Equals(other.memberInfo) && inherit.Equals(other.inherit); |
| 104 | + } |
114 | 105 |
|
115 |
| - public override int GetHashCode() |
| 106 | + public override bool Equals(object? obj) |
| 107 | + { |
| 108 | + if (ReferenceEquals(null, obj)) return false; |
| 109 | + return obj is MemberInfoKey key && Equals(key); |
| 110 | + } |
| 111 | + |
| 112 | + public override int GetHashCode() |
| 113 | + { |
| 114 | + unchecked |
116 | 115 | {
|
117 |
| - unchecked |
118 |
| - { |
119 |
| - return (memberInfo.GetHashCode()*397) ^ inherit.GetHashCode(); |
120 |
| - } |
| 116 | + return (memberInfo.GetHashCode()*397) ^ inherit.GetHashCode(); |
121 | 117 | }
|
122 | 118 | }
|
123 | 119 | }
|
|
0 commit comments