From a88f914446b5a005bab872c510758e681844612a Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Tue, 10 Dec 2019 18:13:39 -0600 Subject: [PATCH] Added some checks for ReflectionUtil --- IPA.Loader/Utilities/ReflectionUtil.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/IPA.Loader/Utilities/ReflectionUtil.cs b/IPA.Loader/Utilities/ReflectionUtil.cs index d7d1af24..b2746c19 100644 --- a/IPA.Loader/Utilities/ReflectionUtil.cs +++ b/IPA.Loader/Utilities/ReflectionUtil.cs @@ -18,6 +18,7 @@ namespace IPA.Utilities public static void SetField(this object obj, string fieldName, object value) { var prop = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + if (prop == null) throw new ArgumentException($"Field {fieldName} does not exist", nameof(fieldName)); prop?.SetValue(obj, value); } @@ -31,6 +32,7 @@ namespace IPA.Utilities public static void SetField(this T obj, string fieldName, object value) where T : class { var prop = typeof(T).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + if (prop == null) throw new ArgumentException($"Field {fieldName} does not exist", nameof(fieldName)); prop?.SetValue(obj, value); } @@ -44,7 +46,8 @@ namespace IPA.Utilities public static T GetField(this object obj, string fieldName) { var prop = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); - var value = prop?.GetValue(obj); + if (prop == null) throw new ArgumentException($"Field {fieldName} does not exist", nameof(fieldName)); + var value = prop?.GetValue(obj); return (T) value; } @@ -57,7 +60,8 @@ namespace IPA.Utilities public static void SetProperty(this object obj, string propertyName, object value) { var prop = obj.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - prop?.SetValue(obj, value, null); + if (prop == null) throw new ArgumentException($"Property {propertyName} does not exist", nameof(propertyName)); + prop?.SetValue(obj, value, null); } /// @@ -70,6 +74,7 @@ namespace IPA.Utilities public static void SetProperty(this T obj, string propertyName, object value) where T : class { var prop = typeof(T).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + if (prop == null) throw new ArgumentException($"Property {propertyName} does not exist", nameof(propertyName)); prop?.SetValue(obj, value, null); } @@ -83,6 +88,7 @@ namespace IPA.Utilities public static object InvokeMethod(this object obj, string methodName, params object[] methodArgs) { MethodInfo dynMethod = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + if (dynMethod == null) throw new ArgumentException($"Method {methodName} does not exist", nameof(methodName)); return dynMethod?.Invoke(obj, methodArgs); } @@ -97,6 +103,7 @@ namespace IPA.Utilities public static object InvokeMethod(this T obj, string methodName, params object[] args) where T : class { var dynMethod = typeof(T).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + if (dynMethod == null) throw new ArgumentException($"Method {methodName} does not exist", nameof(methodName)); return dynMethod?.Invoke(obj, args); }