// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; using System.Linq; using System.Reflection; namespace Microsoft.ClearScript { /// /// Represents a custom attribute loader. /// public class CustomAttributeLoader { // ReSharper disable EmptyConstructor /// /// Initializes a new instance. /// public CustomAttributeLoader() { // the help file builder (SHFB) insists on an empty constructor here } // ReSharper restore EmptyConstructor /// /// Loads custom attributes of the specified type for the given resource. /// /// The type, or a base type, of the custom attributes to load. /// The resource for which to load custom attributes of type . /// True to include custom attributes of type defined for ancestors of , false otherwise. /// An array of custom attributes of type .. /// /// This method is performance-critical. Overrides must not invoke script engine methods or /// other ClearScript functionality. The base implementation loads custom attributes via /// reflection. /// public virtual T[] LoadCustomAttributes(ICustomAttributeProvider resource, bool inherit) where T : Attribute { if (resource is MemberInfo member) { return Attribute.GetCustomAttributes(member, typeof(T), inherit).OfType().ToArray(); } if (resource is ParameterInfo parameter) { return Attribute.GetCustomAttributes(parameter, typeof(T), inherit).OfType().ToArray(); } if (resource is Assembly assembly) { return Attribute.GetCustomAttributes(assembly, typeof(T), inherit).OfType().ToArray(); } if (resource is Module module) { return Attribute.GetCustomAttributes(module, typeof(T), inherit).OfType().ToArray(); } return resource.GetCustomAttributes(typeof(T), inherit).OfType().ToArray(); } } }