diff --git a/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs b/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs index a925434152..cf6ed80411 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/SourceGeneration/SourceGeneratedReflectionOperations.cs @@ -287,24 +287,52 @@ private static bool TryInvokeMatchingConstructor( public bool IsAttributeDefined(ICustomAttributeProvider attributeProvider) where TAttribute : Attribute - => attributeProvider is null - ? throw new ArgumentNullException(nameof(attributeProvider)) - : GetCustomAttributesCached(attributeProvider).OfType().Any(); + { + foreach (Attribute attr in GetCustomAttributesCached(attributeProvider)) + { + if (attr is TAttribute) + { + return true; + } + } + + return false; + } public TAttribute? GetFirstAttributeOrDefault(ICustomAttributeProvider attributeProvider) where TAttribute : Attribute - => GetCustomAttributesCached(attributeProvider).OfType().FirstOrDefault(); + { + foreach (Attribute attr in GetCustomAttributesCached(attributeProvider)) + { + if (attr is TAttribute match) + { + return match; + } + } + + return null; + } public TAttribute? GetSingleAttributeOrDefault(ICustomAttributeProvider attributeProvider) where TAttribute : Attribute { - TAttribute[] matches = [.. GetCustomAttributesCached(attributeProvider).OfType().Take(2)]; - return matches.Length switch + TAttribute? first = null; + foreach (Attribute attr in GetCustomAttributesCached(attributeProvider)) { - 0 => null, - 1 => matches[0], - _ => throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Found multiple attributes of type '{0}' when only one was expected.", typeof(TAttribute))), - }; + if (attr is not TAttribute match) + { + continue; + } + + if (first is not null) + { + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Found multiple attributes of type '{0}' when only one was expected.", typeof(TAttribute))); + } + + first = match; + } + + return first; } public IEnumerable GetAttributes(ICustomAttributeProvider attributeProvider)