From 4afef3695b4b3ae5cea34a32510fb2f06c2079e0 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sat, 28 Sep 2019 10:55:24 +0200 Subject: [PATCH] test utils --- test/WireMock.Net.Tests/TestUtils.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/WireMock.Net.Tests/TestUtils.cs b/test/WireMock.Net.Tests/TestUtils.cs index bd804944..5c8b896d 100644 --- a/test/WireMock.Net.Tests/TestUtils.cs +++ b/test/WireMock.Net.Tests/TestUtils.cs @@ -52,12 +52,19 @@ namespace WireMock.Net.Tests public static void SetPrivatePropertyValue(this object obj, string propertyName, T value) { Type t = obj.GetType(); - if (t.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null) + PropertyInfo propertyInfo = null; + while (propertyInfo == null && t != null) { - throw new ArgumentOutOfRangeException(nameof(propertyName), $"Property {propertyName} was not found in Type {obj.GetType().FullName}"); + propertyInfo = t.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + t = t.BaseType; } - t.InvokeMember(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { value }); + if (propertyInfo == null) + { + throw new ArgumentOutOfRangeException(nameof(propertyName), $"Private property {propertyName} was not found in Type {obj.GetType().FullName}"); + } + + propertyInfo.SetValue(obj, value); } } } \ No newline at end of file