Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
IAttributeRegistry.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 registry for all attributes.
12  /// </summary>
13  public interface IAttributeRegistry
14  {
15  /// <summary>
16  /// Gets the attributes associated with the specified member.
17  /// </summary>
18  /// <param name="memberInfo">The reflection member.</param>
19  /// <param name="inherit">if set to <c>true</c> includes inherited attributes.</param>
20  /// <returns>An enumeration of <see cref="Attribute"/>.</returns>
21  IReadOnlyCollection<Attribute> GetAttributes(MemberInfo memberInfo, bool inherit = true);
22 
23  /// <summary>
24  /// Registers an attribute for the specified member. Restriction: Attributes registered this way cannot be listed in inherited attributes.
25  /// </summary>
26  /// <param name="memberInfo">The member information.</param>
27  /// <param name="attribute">The attribute.</param>
28  void Register(MemberInfo memberInfo, Attribute attribute);
29  }
30 
31  /// <summary>
32  /// Extension methods for attribute registry.
33  /// </summary>
34  public static class AttributeRegistryExtensions
35  {
36  /// <summary>
37  /// Gets the attributes associated with the specified member.
38  /// </summary>
39  /// <typeparam name="T">Type of the attribute</typeparam>
40  /// <param name="attributeRegistry">The attribute registry.</param>
41  /// <param name="memberInfo">The member information.</param>
42  /// <param name="inherit">if set to <c>true</c> [inherit].</param>
43  /// <returns>An enumeration of <see cref="Attribute" />.</returns>
44  public static IEnumerable<T> GetAttributes<T>(this IAttributeRegistry attributeRegistry, MemberInfo memberInfo, bool inherit = true) where T : Attribute
45  {
46  return attributeRegistry.GetAttributes(memberInfo, inherit).OfType<T>();
47  }
48 
49  /// <summary>
50  /// Gets an attribute associated with the specified member.
51  /// </summary>
52  /// <typeparam name="T">Type of the attribute</typeparam>
53  /// <param name="attributeRegistry">The attribute registry.</param>
54  /// <param name="memberInfo">The member information.</param>
55  /// <param name="inherit">if set to <c>true</c> [inherit].</param>
56  /// <returns>An attribute of type {T} if it was found; otherwise <c>null</c></returns>
57  public static T GetAttribute<T>(this IAttributeRegistry attributeRegistry, MemberInfo memberInfo, bool inherit = true) where T : Attribute
58  {
59  var list = attributeRegistry.GetAttributes(memberInfo, inherit);
60  return list.OfType<T>().FirstOrDefault();
61  }
62  }
63 }
Extension methods for attribute registry.