diff --git a/src/Autofac/Core/Activators/Reflection/ReflectionActivator.cs b/src/Autofac/Core/Activators/Reflection/ReflectionActivator.cs index 5c3aabcd6..ef88d2a4a 100644 --- a/src/Autofac/Core/Activators/Reflection/ReflectionActivator.cs +++ b/src/Autofac/Core/Activators/Reflection/ReflectionActivator.cs @@ -81,17 +81,20 @@ public void ConfigurePipeline(IComponentRegistryServices componentRegistryServic #if NET7_0_OR_GREATER // The RequiredMemberAttribute has Inherit = false on its AttributeUsage options, // so we can't use the expected GetCustomAttribute(inherit: true) option, and must walk the tree. - var currentType = _implementationType; - while (currentType is not null && !currentType.Equals(typeof(object))) - { - if (currentType.GetCustomAttribute() is not null) + _anyRequiredMembers = ReflectionCacheSet.Shared.Internal.HasRequiredMemberAttribute.GetOrAdd( + _implementationType, + static t => { - _anyRequiredMembers = true; - break; - } + for (var currentType = t; currentType is not null && currentType != typeof(object); currentType = currentType.BaseType) + { + if (currentType.GetCustomAttribute() is not null) + { + return true; + } + } - currentType = currentType.BaseType; - } + return false; + }); #else _anyRequiredMembers = false; #endif diff --git a/src/Autofac/Core/InternalReflectionCaches.cs b/src/Autofac/Core/InternalReflectionCaches.cs index 2dffafb28..b1068941d 100644 --- a/src/Autofac/Core/InternalReflectionCaches.cs +++ b/src/Autofac/Core/InternalReflectionCaches.cs @@ -59,6 +59,13 @@ internal class InternalReflectionCaches /// public ReflectionCacheDictionary GenericTypeDefinitionByType { get; } +#if NET7_0_OR_GREATER + /// + /// Gets a cache used by . + /// + public ReflectionCacheDictionary HasRequiredMemberAttribute { get; } +#endif + /// /// Initializes a new instance of the class. /// @@ -78,5 +85,8 @@ public InternalReflectionCaches(ReflectionCacheSet set) AutowiringInjectableProperties = set.GetOrCreateCache>>(nameof(AutowiringInjectableProperties)); DefaultPublicConstructors = set.GetOrCreateCache>(nameof(DefaultPublicConstructors)); GenericTypeDefinitionByType = set.GetOrCreateCache>(nameof(GenericTypeDefinitionByType)); +#if NET7_0_OR_GREATER + HasRequiredMemberAttribute = set.GetOrCreateCache>(nameof(HasRequiredMemberAttribute)); +#endif } }