<PackageReference Include="NUnit" Version="3.0.0-rc-2" />

PropertyConstraint

PropertyConstraint extracts a named property and uses its value as the actual value for a chained constraint.
using System; using System.Reflection; namespace NUnit.Framework.Constraints { public class PropertyConstraint : PrefixConstraint { private readonly string name; private object propValue; public PropertyConstraint(string name, IConstraint baseConstraint) : base(baseConstraint) { this.name = name; descriptionPrefix = "property " + name; } public override ConstraintResult ApplyTo<TActual>(TActual actual) { Guard.ArgumentNotNull(actual, "actual"); Type type = actual as Type; if (type == (Type)null) type = actual.GetType(); PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (property == (PropertyInfo)null) throw new ArgumentException($"""{name}""", "name"); propValue = property.GetValue(actual, null); return new ConstraintResult(this, propValue, baseConstraint.ApplyTo(propValue).IsSuccess); } protected override string GetStringRepresentation() { return $"""{name}""{baseConstraint}"""; } } }