From d65346aacd83c9b97b3d533b35a1c98d75169732 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sat, 4 Jan 2020 17:36:00 -0600 Subject: [PATCH] ReflectionUtil documentation --- IPA.Loader/Utilities/ReflectionUtil.cs | 42 +++++++++++++++++++++----- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/IPA.Loader/Utilities/ReflectionUtil.cs b/IPA.Loader/Utilities/ReflectionUtil.cs index 2c563159..4ab49312 100644 --- a/IPA.Loader/Utilities/ReflectionUtil.cs +++ b/IPA.Loader/Utilities/ReflectionUtil.cs @@ -15,6 +15,7 @@ namespace IPA.Utilities /// the object instance /// the field to set /// the value to set it to + /// if does not exist on the runtime type of public static void SetField(this object obj, string fieldName, object value) { var prop = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -29,13 +30,14 @@ namespace IPA.Utilities /// the object instance /// the field to set /// the value to set it to - public static void SetField(this T obj, string fieldName, object value) where T : class + /// if does not exist on + public static void SetField(this T obj, string fieldName, object value) { 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); } - + /// /// Gets the value of a field. /// @@ -43,6 +45,7 @@ namespace IPA.Utilities /// the object instance to pull from /// the name of the field to read /// the value of the field + /// if does not exist on the runtime type of public static T GetField(this object obj, string fieldName) { var prop = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); @@ -50,13 +53,31 @@ namespace IPA.Utilities var value = prop?.GetValue(obj); return (T) value; } - + + /// + /// Gets the value of a field. + /// + /// the type of the field (result casted) + /// the type to get the field from + /// the object instance to pull from + /// the name of the field to read + /// the value of the field + /// if does not exist on + public static T GetField(this U obj, string fieldName) + { + var prop = typeof(U).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); + if (prop == null) throw new ArgumentException($"Field {fieldName} does not exist", nameof(fieldName)); + var value = prop?.GetValue(obj); + return (T)value; + } + /// /// Sets a property on the target object. /// /// the target object instance /// the name of the property /// the value to set it to + /// if does not exist on the runtime type of public static void SetProperty(this object obj, string propertyName, object value) { var prop = obj.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -71,7 +92,8 @@ namespace IPA.Utilities /// the object instance /// the property to set /// the value to set it to - public static void SetProperty(this T obj, string propertyName, object value) where T : class + /// if does not exist on + public static void SetProperty(this T obj, string propertyName, object value) { 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)); @@ -85,6 +107,7 @@ namespace IPA.Utilities /// the method name /// the method arguments /// the return value + /// if does not exist on the runtime type of public static object InvokeMethod(this object obj, string methodName, params object[] methodArgs) { MethodInfo dynMethod = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -100,7 +123,8 @@ namespace IPA.Utilities /// the method's name /// the method arguments /// the return value - public static object InvokeMethod(this T obj, string methodName, params object[] args) where T : class + /// if does not exist on + public static object InvokeMethod(this T obj, string methodName, params object[] args) { 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)); @@ -115,6 +139,8 @@ namespace IPA.Utilities /// the method name to call /// the method's arguments /// the return value + /// if does not exist on the runtime type of + /// public static T InvokeMethod(this object obj, string methodName, params object[] methodArgs) => (T)InvokeMethod(obj, methodName, methodArgs); @@ -127,8 +153,10 @@ namespace IPA.Utilities /// the method name to call /// the method's arguments /// the return value - public static T InvokeMethod(this U obj, string methodName, params object[] methodArgs) where U : class - => (T)obj.InvokeMethod(methodName, methodArgs); + /// if does not exist on + /// + public static T InvokeMethod(this U obj, string methodName, params object[] methodArgs) + => (T)InvokeMethod(obj, methodName, methodArgs); /// /// Copies a component to a component of on the destination .