using System.Reflection; namespace OpenNest.WinForms.Tests; /// Reads and sets private members from tests. Import with using static. internal static class TestReflection { /// The named private instance field declared by the owner's own type. public static T Field(object owner, string name) => Field(owner.GetType(), owner, name); /// The named private instance field declared by . public static T Field(Type type, object owner, string name) => (T)type.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(owner)!; /// Sets the compiler-generated backing field of an auto-property. public static void SetBackingField(object target, string property, object value) => target.GetType() .GetField( $"<{property}>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic)! .SetValue(target, value); }