AttributeProviderWrapper<T>
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class AttributeProviderWrapper<[System.Runtime.CompilerServices.Nullable(0)] T> : ICustomAttributeProvider where T : Attribute
{
private readonly ICustomAttributeProvider _innerProvider;
public AttributeProviderWrapper(ICustomAttributeProvider innerProvider)
{
_innerProvider = innerProvider;
}
public object[] GetCustomAttributes(Type attributeType, bool inherit)
{
object[] customAttributes = _innerProvider.GetCustomAttributes(attributeType, inherit);
return GetFiltered(customAttributes);
}
public object[] GetCustomAttributes(bool inherit)
{
throw new InvalidOperationException("Should not be called as 3rd party attributes could throw exceptions");
}
public bool IsDefined(Type attributeType, bool inherit)
{
return GetCustomAttributes(attributeType, inherit).Length != 0;
}
private static T[] GetFiltered(object[] attributes)
{
List<T> list = null;
foreach (object obj in attributes) {
T val = obj as T;
if (val != null) {
if (list == null)
list = new List<T>();
list.Add(val);
}
}
return list?.ToArray() ?? Array.Empty<T>();
}
}
}