<PackageReference Include="NUnit" Version="4.4.0-beta.1" />

AttributeConstraint

AttributeConstraint tests that a specified attribute is present on a Type or other provider and that the value of the attribute satisfies some other constraint.
using NUnit.Compatibility; using System; using System.Runtime.CompilerServices; namespace NUnit.Framework.Constraints { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class AttributeConstraint : PrefixConstraint { private readonly Type _expectedType; public AttributeConstraint(Type type, IConstraint baseConstraint) : base(baseConstraint, "attribute " + type.FullName) { _expectedType = type; if (!typeof(Attribute).IsAssignableFrom(_expectedType)) throw new ArgumentException($"""{_expectedType}""", "type"); } public override ConstraintResult ApplyTo<[System.Runtime.CompilerServices.Nullable(2)] TActual>(TActual actual) { Guard.ArgumentNotNull(actual, "actual"); Attribute[] customAttributes = AttributeHelper.GetCustomAttributes(actual, _expectedType, true); if (customAttributes.Length == 0) throw new ArgumentException($"""{_expectedType}""", "actual"); Attribute actual2 = customAttributes[0]; return base.BaseConstraint.ApplyTo(actual2); } protected override string GetStringRepresentation() { return $"""{_expectedType}""{base.BaseConstraint}"""; } } }