From 0def9a85a79d67dd76655bef988e77f2e15ab1db Mon Sep 17 00:00:00 2001 From: Zingabopp Date: Sat, 4 Jan 2020 12:16:02 -0600 Subject: [PATCH] SetPrivateProperty with target Type overload --- IPA.Loader/Utilities/ReflectionUtil.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/IPA.Loader/Utilities/ReflectionUtil.cs b/IPA.Loader/Utilities/ReflectionUtil.cs index e5b0650e..21badc6a 100644 --- a/IPA.Loader/Utilities/ReflectionUtil.cs +++ b/IPA.Loader/Utilities/ReflectionUtil.cs @@ -28,7 +28,7 @@ namespace IPA.Utilities /// the object instance /// the field to set /// the value to set it to - /// the object type the field belongs to + /// the object the field belongs to /// thrown when is not a member of /// thrown when isn't assignable as public static void SetPrivateField(this object obj, string fieldName, object value, Type targetType) @@ -54,12 +54,12 @@ namespace IPA.Utilities } /// - /// Gets the value of a (potentially) private field. specifies the the field belongs to + /// Gets the value of a (potentially) private field. specifies the the field belongs to. /// /// the type of the field (result casted) /// the object instance to pull from /// the name of the field to read - /// the object type the field belongs to + /// the object the field belongs to /// the value of the field /// thrown when is not a member of /// thrown when isn't assignable as @@ -82,12 +82,26 @@ namespace IPA.Utilities public static void SetPrivateProperty(this object obj, string propertyName, object value) { Type targetType = obj.GetType(); + obj.SetPrivateProperty(propertyName, value, targetType); + } + + /// + /// Sets a (potentially) private property on the target object. + /// + /// the target object instance + /// the name of the property + /// the value to set it to + /// the object the property belongs to + /// thrown when is not a member of + /// thrown when isn't assignable as + public static void SetPrivateProperty(this object obj, string propertyName, object value, Type targetType) + { var prop = targetType .GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (prop == null) throw new InvalidOperationException($"{propertyName} is not a member of {targetType.Name}"); prop.SetValue(obj, value, null); - } + } /// /// Invokes a (potentially) private method.