<PackageReference Include="NUnit" Version="4.3.1" />

PropertyExistsConstraint

PropertyExistsConstraint tests that a named property exists on the object provided through Match. Originally, PropertyConstraint provided this feature in addition to making optional tests on the value of the property. The two constraints are now separate.
using NUnit.Framework.Internal; using System; using System.Reflection; using System.Runtime.CompilerServices; namespace NUnit.Framework.Constraints { [NullableContext(1)] [Nullable(0)] public class PropertyExistsConstraint : Constraint { private readonly string _name; [Nullable(2)] private Type _actualType; public override string Description => "property " + _name; public PropertyExistsConstraint(string name) : base(name) { _name = name; } public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(TActual actual) { Guard.ArgumentNotNull(actual, "actual"); _actualType = typeof(TActual); PropertyInfo ultimateShadowingProperty = Reflect.GetUltimateShadowingProperty(_actualType, _name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if ((object)ultimateShadowingProperty == null && typeof(TActual).IsInterface) { Type[] interfaces = typeof(TActual).GetInterfaces(); foreach (Type type in interfaces) { ultimateShadowingProperty = Reflect.GetUltimateShadowingProperty(type, _name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if ((object)ultimateShadowingProperty != null) break; } } if ((object)ultimateShadowingProperty == null) { Type type2 = actual as Type; if ((object)type2 != null) { _actualType = type2; ultimateShadowingProperty = Reflect.GetUltimateShadowingProperty(_actualType, _name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if ((object)ultimateShadowingProperty == null) ultimateShadowingProperty = Reflect.GetUltimateShadowingProperty(actual.GetType(), _name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } else { _actualType = actual.GetType(); ultimateShadowingProperty = Reflect.GetUltimateShadowingProperty(_actualType, _name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } } return new ConstraintResult(this, _actualType, (object)ultimateShadowingProperty != null); } protected override string GetStringRepresentation() { return "<propertyexists " + _name + ">"; } } }