Files
OpenNest/OpenNest.WinForms.Tests/TestReflection.cs
T
ajandClaude Opus 5.5 c027793077 test(desktop): share form pumping and reflection helpers
Desktop test classes each kept identical PumpUntil, Show, Mouse, All,
PressKey, Raise, SetBackingField and Field helpers. They now import
FormTesting and TestReflection; the two Field variants that read a base
type's field or unwrap DrawingLibraryForm keep a one-line wrapper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 11:24:46 -05:00

23 lines
1.0 KiB
C#

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