From 8776d1afa6dce054f3bc7cb421c86f3e2fe06ab3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 10 Dec 2017 18:05:18 -0500 Subject: adjust reflection API to correctly reflect what it does (#410) --- src/SMAPI/Framework/Reflection/PrivateField.cs | 93 ------------------ src/SMAPI/Framework/Reflection/PrivateMethod.cs | 99 ------------------- src/SMAPI/Framework/Reflection/PrivateProperty.cs | 105 -------------------- src/SMAPI/Framework/Reflection/ReflectedField.cs | 93 ++++++++++++++++++ src/SMAPI/Framework/Reflection/ReflectedMethod.cs | 99 +++++++++++++++++++ .../Framework/Reflection/ReflectedProperty.cs | 105 ++++++++++++++++++++ src/SMAPI/Framework/Reflection/Reflector.cs | 106 ++++++++++----------- 7 files changed, 350 insertions(+), 350 deletions(-) delete mode 100644 src/SMAPI/Framework/Reflection/PrivateField.cs delete mode 100644 src/SMAPI/Framework/Reflection/PrivateMethod.cs delete mode 100644 src/SMAPI/Framework/Reflection/PrivateProperty.cs create mode 100644 src/SMAPI/Framework/Reflection/ReflectedField.cs create mode 100644 src/SMAPI/Framework/Reflection/ReflectedMethod.cs create mode 100644 src/SMAPI/Framework/Reflection/ReflectedProperty.cs (limited to 'src/SMAPI/Framework/Reflection') diff --git a/src/SMAPI/Framework/Reflection/PrivateField.cs b/src/SMAPI/Framework/Reflection/PrivateField.cs deleted file mode 100644 index 0bf45969..00000000 --- a/src/SMAPI/Framework/Reflection/PrivateField.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.Reflection; - -namespace StardewModdingAPI.Framework.Reflection -{ - /// A private field obtained through reflection. - /// The field value type. - internal class PrivateField : IPrivateField - { - /********* - ** Properties - *********/ - /// The type that has the field. - private readonly Type ParentType; - - /// The object that has the instance field (if applicable). - private readonly object Parent; - - /// The display name shown in error messages. - private string DisplayName => $"{this.ParentType.FullName}::{this.FieldInfo.Name}"; - - - /********* - ** Accessors - *********/ - /// The reflection metadata. - public FieldInfo FieldInfo { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The type that has the field. - /// The object that has the instance field (if applicable). - /// The reflection metadata. - /// Whether the field is static. - /// The or is null. - /// The is null for a non-static field, or not null for a static field. - public PrivateField(Type parentType, object obj, FieldInfo field, bool isStatic) - { - // validate - if (parentType == null) - throw new ArgumentNullException(nameof(parentType)); - if (field == null) - throw new ArgumentNullException(nameof(field)); - if (isStatic && obj != null) - throw new ArgumentException("A static field cannot have an object instance."); - if (!isStatic && obj == null) - throw new ArgumentException("A non-static field must have an object instance."); - - // save - this.ParentType = parentType; - this.Parent = obj; - this.FieldInfo = field; - } - - /// Get the field value. - public TValue GetValue() - { - try - { - return (TValue)this.FieldInfo.GetValue(this.Parent); - } - catch (InvalidCastException) - { - throw new InvalidCastException($"Can't convert the private {this.DisplayName} field from {this.FieldInfo.FieldType.FullName} to {typeof(TValue).FullName}."); - } - catch (Exception ex) - { - throw new Exception($"Couldn't get the value of the private {this.DisplayName} field", ex); - } - } - - /// Set the field value. - //// The value to set. - public void SetValue(TValue value) - { - try - { - this.FieldInfo.SetValue(this.Parent, value); - } - catch (InvalidCastException) - { - throw new InvalidCastException($"Can't assign the private {this.DisplayName} field a {typeof(TValue).FullName} value, must be compatible with {this.FieldInfo.FieldType.FullName}."); - } - catch (Exception ex) - { - throw new Exception($"Couldn't set the value of the private {this.DisplayName} field", ex); - } - } - } -} diff --git a/src/SMAPI/Framework/Reflection/PrivateMethod.cs b/src/SMAPI/Framework/Reflection/PrivateMethod.cs deleted file mode 100644 index ba2374f4..00000000 --- a/src/SMAPI/Framework/Reflection/PrivateMethod.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Reflection; - -namespace StardewModdingAPI.Framework.Reflection -{ - /// A private method obtained through reflection. - internal class PrivateMethod : IPrivateMethod - { - /********* - ** Properties - *********/ - /// The type that has the method. - private readonly Type ParentType; - - /// The object that has the instance method (if applicable). - private readonly object Parent; - - /// The display name shown in error messages. - private string DisplayName => $"{this.ParentType.FullName}::{this.MethodInfo.Name}"; - - - /********* - ** Accessors - *********/ - /// The reflection metadata. - public MethodInfo MethodInfo { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The type that has the method. - /// The object that has the instance method(if applicable). - /// The reflection metadata. - /// Whether the field is static. - /// The or is null. - /// The is null for a non-static method, or not null for a static method. - public PrivateMethod(Type parentType, object obj, MethodInfo method, bool isStatic) - { - // validate - if (parentType == null) - throw new ArgumentNullException(nameof(parentType)); - if (method == null) - throw new ArgumentNullException(nameof(method)); - if (isStatic && obj != null) - throw new ArgumentException("A static method cannot have an object instance."); - if (!isStatic && obj == null) - throw new ArgumentException("A non-static method must have an object instance."); - - // save - this.ParentType = parentType; - this.Parent = obj; - this.MethodInfo = method; - } - - /// Invoke the method. - /// The return type. - /// The method arguments to pass in. - public TValue Invoke(params object[] arguments) - { - // invoke method - object result; - try - { - result = this.MethodInfo.Invoke(this.Parent, arguments); - } - catch (Exception ex) - { - throw new Exception($"Couldn't invoke the private {this.DisplayName} field", ex); - } - - // cast return value - try - { - return (TValue)result; - } - catch (InvalidCastException) - { - throw new InvalidCastException($"Can't convert the return value of the private {this.DisplayName} method from {this.MethodInfo.ReturnType.FullName} to {typeof(TValue).FullName}."); - } - } - - /// Invoke the method. - /// The method arguments to pass in. - public void Invoke(params object[] arguments) - { - // invoke method - try - { - this.MethodInfo.Invoke(this.Parent, arguments); - } - catch (Exception ex) - { - throw new Exception($"Couldn't invoke the private {this.DisplayName} field", ex); - } - } - } -} \ No newline at end of file diff --git a/src/SMAPI/Framework/Reflection/PrivateProperty.cs b/src/SMAPI/Framework/Reflection/PrivateProperty.cs deleted file mode 100644 index c81f99c7..00000000 --- a/src/SMAPI/Framework/Reflection/PrivateProperty.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Reflection; - -namespace StardewModdingAPI.Framework.Reflection -{ - /// A private property obtained through reflection. - /// The property value type. - internal class PrivateProperty : IPrivateProperty - { - /********* - ** Properties - *********/ - /// The display name shown in error messages. - private readonly string DisplayName; - - /// The underlying property getter. - private readonly Func GetMethod; - - /// The underlying property setter. - private readonly Action SetMethod; - - - /********* - ** Accessors - *********/ - /// The reflection metadata. - public PropertyInfo PropertyInfo { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The type that has the field. - /// The object that has the instance field (if applicable). - /// The reflection metadata. - /// Whether the field is static. - /// The or is null. - /// The is null for a non-static field, or not null for a static field. - public PrivateProperty(Type parentType, object obj, PropertyInfo property, bool isStatic) - { - // validate input - if (parentType == null) - throw new ArgumentNullException(nameof(parentType)); - if (property == null) - throw new ArgumentNullException(nameof(property)); - - // validate static - if (isStatic && obj != null) - throw new ArgumentException("A static property cannot have an object instance."); - if (!isStatic && obj == null) - throw new ArgumentException("A non-static property must have an object instance."); - - - this.DisplayName = $"{parentType.FullName}::{property.Name}"; - this.PropertyInfo = property; - - if (this.PropertyInfo.GetMethod != null) - this.GetMethod = (Func)Delegate.CreateDelegate(typeof(Func), obj, this.PropertyInfo.GetMethod); - if (this.PropertyInfo.SetMethod != null) - this.SetMethod = (Action)Delegate.CreateDelegate(typeof(Action), obj, this.PropertyInfo.SetMethod); - } - - /// Get the property value. - public TValue GetValue() - { - if (this.GetMethod == null) - throw new InvalidOperationException($"The private {this.DisplayName} property has no get method."); - - try - { - return this.GetMethod(); - } - catch (InvalidCastException) - { - throw new InvalidCastException($"Can't convert the private {this.DisplayName} property from {this.PropertyInfo.PropertyType.FullName} to {typeof(TValue).FullName}."); - } - catch (Exception ex) - { - throw new Exception($"Couldn't get the value of the private {this.DisplayName} property", ex); - } - } - - /// Set the property value. - //// The value to set. - public void SetValue(TValue value) - { - if (this.SetMethod == null) - throw new InvalidOperationException($"The private {this.DisplayName} property has no set method."); - - try - { - this.SetMethod(value); - } - catch (InvalidCastException) - { - throw new InvalidCastException($"Can't assign the private {this.DisplayName} property a {typeof(TValue).FullName} value, must be compatible with {this.PropertyInfo.PropertyType.FullName}."); - } - catch (Exception ex) - { - throw new Exception($"Couldn't set the value of the private {this.DisplayName} property", ex); - } - } - } -} diff --git a/src/SMAPI/Framework/Reflection/ReflectedField.cs b/src/SMAPI/Framework/Reflection/ReflectedField.cs new file mode 100644 index 00000000..ad1557bb --- /dev/null +++ b/src/SMAPI/Framework/Reflection/ReflectedField.cs @@ -0,0 +1,93 @@ +using System; +using System.Reflection; + +namespace StardewModdingAPI.Framework.Reflection +{ + /// A field obtained through reflection. + /// The field value type. + internal class ReflectedField : IPrivateField, IReflectedField + { + /********* + ** Properties + *********/ + /// The type that has the field. + private readonly Type ParentType; + + /// The object that has the instance field (if applicable). + private readonly object Parent; + + /// The display name shown in error messages. + private string DisplayName => $"{this.ParentType.FullName}::{this.FieldInfo.Name}"; + + + /********* + ** Accessors + *********/ + /// The reflection metadata. + public FieldInfo FieldInfo { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The type that has the field. + /// The object that has the instance field (if applicable). + /// The reflection metadata. + /// Whether the field is static. + /// The or is null. + /// The is null for a non-static field, or not null for a static field. + public ReflectedField(Type parentType, object obj, FieldInfo field, bool isStatic) + { + // validate + if (parentType == null) + throw new ArgumentNullException(nameof(parentType)); + if (field == null) + throw new ArgumentNullException(nameof(field)); + if (isStatic && obj != null) + throw new ArgumentException("A static field cannot have an object instance."); + if (!isStatic && obj == null) + throw new ArgumentException("A non-static field must have an object instance."); + + // save + this.ParentType = parentType; + this.Parent = obj; + this.FieldInfo = field; + } + + /// Get the field value. + public TValue GetValue() + { + try + { + return (TValue)this.FieldInfo.GetValue(this.Parent); + } + catch (InvalidCastException) + { + throw new InvalidCastException($"Can't convert the {this.DisplayName} field from {this.FieldInfo.FieldType.FullName} to {typeof(TValue).FullName}."); + } + catch (Exception ex) + { + throw new Exception($"Couldn't get the value of the {this.DisplayName} field", ex); + } + } + + /// Set the field value. + //// The value to set. + public void SetValue(TValue value) + { + try + { + this.FieldInfo.SetValue(this.Parent, value); + } + catch (InvalidCastException) + { + throw new InvalidCastException($"Can't assign the {this.DisplayName} field a {typeof(TValue).FullName} value, must be compatible with {this.FieldInfo.FieldType.FullName}."); + } + catch (Exception ex) + { + throw new Exception($"Couldn't set the value of the {this.DisplayName} field", ex); + } + } + } +} diff --git a/src/SMAPI/Framework/Reflection/ReflectedMethod.cs b/src/SMAPI/Framework/Reflection/ReflectedMethod.cs new file mode 100644 index 00000000..376de869 --- /dev/null +++ b/src/SMAPI/Framework/Reflection/ReflectedMethod.cs @@ -0,0 +1,99 @@ +using System; +using System.Reflection; + +namespace StardewModdingAPI.Framework.Reflection +{ + /// A method obtained through reflection. + internal class ReflectedMethod : IPrivateMethod, IReflectedMethod + { + /********* + ** Properties + *********/ + /// The type that has the method. + private readonly Type ParentType; + + /// The object that has the instance method (if applicable). + private readonly object Parent; + + /// The display name shown in error messages. + private string DisplayName => $"{this.ParentType.FullName}::{this.MethodInfo.Name}"; + + + /********* + ** Accessors + *********/ + /// The reflection metadata. + public MethodInfo MethodInfo { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The type that has the method. + /// The object that has the instance method(if applicable). + /// The reflection metadata. + /// Whether the method is static. + /// The or is null. + /// The is null for a non-static method, or not null for a static method. + public ReflectedMethod(Type parentType, object obj, MethodInfo method, bool isStatic) + { + // validate + if (parentType == null) + throw new ArgumentNullException(nameof(parentType)); + if (method == null) + throw new ArgumentNullException(nameof(method)); + if (isStatic && obj != null) + throw new ArgumentException("A static method cannot have an object instance."); + if (!isStatic && obj == null) + throw new ArgumentException("A non-static method must have an object instance."); + + // save + this.ParentType = parentType; + this.Parent = obj; + this.MethodInfo = method; + } + + /// Invoke the method. + /// The return type. + /// The method arguments to pass in. + public TValue Invoke(params object[] arguments) + { + // invoke method + object result; + try + { + result = this.MethodInfo.Invoke(this.Parent, arguments); + } + catch (Exception ex) + { + throw new Exception($"Couldn't invoke the {this.DisplayName} method", ex); + } + + // cast return value + try + { + return (TValue)result; + } + catch (InvalidCastException) + { + throw new InvalidCastException($"Can't convert the return value of the {this.DisplayName} method from {this.MethodInfo.ReturnType.FullName} to {typeof(TValue).FullName}."); + } + } + + /// Invoke the method. + /// The method arguments to pass in. + public void Invoke(params object[] arguments) + { + // invoke method + try + { + this.MethodInfo.Invoke(this.Parent, arguments); + } + catch (Exception ex) + { + throw new Exception($"Couldn't invoke the {this.DisplayName} method", ex); + } + } + } +} diff --git a/src/SMAPI/Framework/Reflection/ReflectedProperty.cs b/src/SMAPI/Framework/Reflection/ReflectedProperty.cs new file mode 100644 index 00000000..d6c964c1 --- /dev/null +++ b/src/SMAPI/Framework/Reflection/ReflectedProperty.cs @@ -0,0 +1,105 @@ +using System; +using System.Reflection; + +namespace StardewModdingAPI.Framework.Reflection +{ + /// A property obtained through reflection. + /// The property value type. + internal class ReflectedProperty : IPrivateProperty, IReflectedProperty + { + /********* + ** Properties + *********/ + /// The display name shown in error messages. + private readonly string DisplayName; + + /// The underlying property getter. + private readonly Func GetMethod; + + /// The underlying property setter. + private readonly Action SetMethod; + + + /********* + ** Accessors + *********/ + /// The reflection metadata. + public PropertyInfo PropertyInfo { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The type that has the property. + /// The object that has the instance property (if applicable). + /// The reflection metadata. + /// Whether the property is static. + /// The or is null. + /// The is null for a non-static property, or not null for a static property. + public ReflectedProperty(Type parentType, object obj, PropertyInfo property, bool isStatic) + { + // validate input + if (parentType == null) + throw new ArgumentNullException(nameof(parentType)); + if (property == null) + throw new ArgumentNullException(nameof(property)); + + // validate static + if (isStatic && obj != null) + throw new ArgumentException("A static property cannot have an object instance."); + if (!isStatic && obj == null) + throw new ArgumentException("A non-static property must have an object instance."); + + + this.DisplayName = $"{parentType.FullName}::{property.Name}"; + this.PropertyInfo = property; + + if (this.PropertyInfo.GetMethod != null) + this.GetMethod = (Func)Delegate.CreateDelegate(typeof(Func), obj, this.PropertyInfo.GetMethod); + if (this.PropertyInfo.SetMethod != null) + this.SetMethod = (Action)Delegate.CreateDelegate(typeof(Action), obj, this.PropertyInfo.SetMethod); + } + + /// Get the property value. + public TValue GetValue() + { + if (this.GetMethod == null) + throw new InvalidOperationException($"The {this.DisplayName} property has no get method."); + + try + { + return this.GetMethod(); + } + catch (InvalidCastException) + { + throw new InvalidCastException($"Can't convert the {this.DisplayName} property from {this.PropertyInfo.PropertyType.FullName} to {typeof(TValue).FullName}."); + } + catch (Exception ex) + { + throw new Exception($"Couldn't get the value of the {this.DisplayName} property", ex); + } + } + + /// Set the property value. + //// The value to set. + public void SetValue(TValue value) + { + if (this.SetMethod == null) + throw new InvalidOperationException($"The {this.DisplayName} property has no set method."); + + try + { + this.SetMethod(value); + } + catch (InvalidCastException) + { + throw new InvalidCastException($"Can't assign the {this.DisplayName} property a {typeof(TValue).FullName} value, must be compatible with {this.PropertyInfo.PropertyType.FullName}."); + } + catch (Exception ex) + { + throw new Exception($"Couldn't set the value of the {this.DisplayName} property", ex); + } + } + } +} diff --git a/src/SMAPI/Framework/Reflection/Reflector.cs b/src/SMAPI/Framework/Reflection/Reflector.cs index 23a48505..910e3a54 100644 --- a/src/SMAPI/Framework/Reflection/Reflector.cs +++ b/src/SMAPI/Framework/Reflection/Reflector.cs @@ -5,7 +5,7 @@ using System.Runtime.Caching; namespace StardewModdingAPI.Framework.Reflection { - /// Provides helper methods for accessing private game code. + /// Provides helper methods for accessing inaccessible code. /// This implementation searches up the type hierarchy, and caches the reflected fields and methods with a sliding expiry (to optimise performance without unnecessary memory usage). internal class Reflector { @@ -25,139 +25,139 @@ namespace StardewModdingAPI.Framework.Reflection /**** ** Fields ****/ - /// Get a private instance field. + /// Get a instance field. /// The field type. /// The object which has the field. /// The field name. - /// Whether to throw an exception if the private field is not found. + /// Whether to throw an exception if the field is not found. /// Returns the field wrapper, or null if the field doesn't exist and is false. - public IPrivateField GetPrivateField(object obj, string name, bool required = true) + public IReflectedField GetField(object obj, string name, bool required = true) { // validate if (obj == null) - throw new ArgumentNullException(nameof(obj), "Can't get a private instance field from a null object."); + throw new ArgumentNullException(nameof(obj), "Can't get a instance field from a null object."); // get field from hierarchy - IPrivateField field = this.GetFieldFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); + IReflectedField field = this.GetFieldFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (required && field == null) - throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance field."); + throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance field."); return field; } - /// Get a private static field. + /// Get a static field. /// The field type. /// The type which has the field. /// The field name. - /// Whether to throw an exception if the private field is not found. - public IPrivateField GetPrivateField(Type type, string name, bool required = true) + /// Whether to throw an exception if the field is not found. + public IReflectedField GetField(Type type, string name, bool required = true) { // get field from hierarchy - IPrivateField field = this.GetFieldFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public); + IReflectedField field = this.GetFieldFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public); if (required && field == null) - throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static field."); + throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static field."); return field; } /**** ** Properties ****/ - /// Get a private instance property. + /// Get a instance property. /// The property type. /// The object which has the property. /// The property name. - /// Whether to throw an exception if the private property is not found. - public IPrivateProperty GetPrivateProperty(object obj, string name, bool required = true) + /// Whether to throw an exception if the property is not found. + public IReflectedProperty GetProperty(object obj, string name, bool required = true) { // validate if (obj == null) - throw new ArgumentNullException(nameof(obj), "Can't get a private instance property from a null object."); + throw new ArgumentNullException(nameof(obj), "Can't get a instance property from a null object."); // get property from hierarchy - IPrivateProperty property = this.GetPropertyFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); + IReflectedProperty property = this.GetPropertyFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (required && property == null) - throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance property."); + throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance property."); return property; } - /// Get a private static property. + /// Get a static property. /// The property type. /// The type which has the property. /// The property name. - /// Whether to throw an exception if the private property is not found. - public IPrivateProperty GetPrivateProperty(Type type, string name, bool required = true) + /// Whether to throw an exception if the property is not found. + public IReflectedProperty GetProperty(Type type, string name, bool required = true) { // get field from hierarchy - IPrivateProperty property = this.GetPropertyFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); + IReflectedProperty property = this.GetPropertyFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); if (required && property == null) - throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static property."); + throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static property."); return property; } /**** ** Methods ****/ - /// Get a private instance method. + /// Get a instance method. /// The object which has the method. /// The field name. - /// Whether to throw an exception if the private field is not found. - public IPrivateMethod GetPrivateMethod(object obj, string name, bool required = true) + /// Whether to throw an exception if the field is not found. + public IReflectedMethod GetMethod(object obj, string name, bool required = true) { // validate if (obj == null) - throw new ArgumentNullException(nameof(obj), "Can't get a private instance method from a null object."); + throw new ArgumentNullException(nameof(obj), "Can't get a instance method from a null object."); // get method from hierarchy - IPrivateMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); + IReflectedMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (required && method == null) - throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance method."); + throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance method."); return method; } - /// Get a private static method. + /// Get a static method. /// The type which has the method. /// The field name. - /// Whether to throw an exception if the private field is not found. - public IPrivateMethod GetPrivateMethod(Type type, string name, bool required = true) + /// Whether to throw an exception if the field is not found. + public IReflectedMethod GetMethod(Type type, string name, bool required = true) { // get method from hierarchy - IPrivateMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); + IReflectedMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); if (required && method == null) - throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static method."); + throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static method."); return method; } /**** ** Methods by signature ****/ - /// Get a private instance method. + /// Get a instance method. /// The object which has the method. /// The field name. /// The argument types of the method signature to find. - /// Whether to throw an exception if the private field is not found. - public IPrivateMethod GetPrivateMethod(object obj, string name, Type[] argumentTypes, bool required = true) + /// Whether to throw an exception if the field is not found. + public IReflectedMethod GetMethod(object obj, string name, Type[] argumentTypes, bool required = true) { // validate parent if (obj == null) - throw new ArgumentNullException(nameof(obj), "Can't get a private instance method from a null object."); + throw new ArgumentNullException(nameof(obj), "Can't get a instance method from a null object."); // get method from hierarchy - PrivateMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, argumentTypes); + ReflectedMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, argumentTypes); if (required && method == null) - throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a private '{name}' instance method with that signature."); + throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance method with that signature."); return method; } - /// Get a private static method. + /// Get a static method. /// The type which has the method. /// The field name. /// The argument types of the method signature to find. - /// Whether to throw an exception if the private field is not found. - public IPrivateMethod GetPrivateMethod(Type type, string name, Type[] argumentTypes, bool required = true) + /// Whether to throw an exception if the field is not found. + public IReflectedMethod GetMethod(Type type, string name, Type[] argumentTypes, bool required = true) { // get field from hierarchy - PrivateMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, argumentTypes); + ReflectedMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, argumentTypes); if (required && method == null) - throw new InvalidOperationException($"The {type.FullName} object doesn't have a private '{name}' static method with that signature."); + throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static method with that signature."); return method; } @@ -171,7 +171,7 @@ namespace StardewModdingAPI.Framework.Reflection /// The object which has the field. /// The field name. /// The reflection binding which flags which indicates what type of field to find. - private IPrivateField GetFieldFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags) + private IReflectedField GetFieldFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags) { bool isStatic = bindingFlags.HasFlag(BindingFlags.Static); FieldInfo field = this.GetCached($"field::{isStatic}::{type.FullName}::{name}", () => @@ -183,7 +183,7 @@ namespace StardewModdingAPI.Framework.Reflection }); return field != null - ? new PrivateField(type, obj, field, isStatic) + ? new ReflectedField(type, obj, field, isStatic) : null; } @@ -193,7 +193,7 @@ namespace StardewModdingAPI.Framework.Reflection /// The object which has the property. /// The property name. /// The reflection binding which flags which indicates what type of property to find. - private IPrivateProperty GetPropertyFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags) + private IReflectedProperty GetPropertyFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags) { bool isStatic = bindingFlags.HasFlag(BindingFlags.Static); PropertyInfo property = this.GetCached($"property::{isStatic}::{type.FullName}::{name}", () => @@ -205,7 +205,7 @@ namespace StardewModdingAPI.Framework.Reflection }); return property != null - ? new PrivateProperty(type, obj, property, isStatic) + ? new ReflectedProperty(type, obj, property, isStatic) : null; } @@ -214,7 +214,7 @@ namespace StardewModdingAPI.Framework.Reflection /// The object which has the method. /// The method name. /// The reflection binding which flags which indicates what type of method to find. - private IPrivateMethod GetMethodFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags) + private IReflectedMethod GetMethodFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags) { bool isStatic = bindingFlags.HasFlag(BindingFlags.Static); MethodInfo method = this.GetCached($"method::{isStatic}::{type.FullName}::{name}", () => @@ -226,7 +226,7 @@ namespace StardewModdingAPI.Framework.Reflection }); return method != null - ? new PrivateMethod(type, obj, method, isStatic: bindingFlags.HasFlag(BindingFlags.Static)) + ? new ReflectedMethod(type, obj, method, isStatic: bindingFlags.HasFlag(BindingFlags.Static)) : null; } @@ -236,7 +236,7 @@ namespace StardewModdingAPI.Framework.Reflection /// The method name. /// The reflection binding which flags which indicates what type of method to find. /// The argument types of the method signature to find. - private PrivateMethod GetMethodFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags, Type[] argumentTypes) + private ReflectedMethod GetMethodFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags, Type[] argumentTypes) { bool isStatic = bindingFlags.HasFlag(BindingFlags.Static); MethodInfo method = this.GetCached($"method::{isStatic}::{type.FullName}::{name}({string.Join(",", argumentTypes.Select(p => p.FullName))})", () => @@ -247,7 +247,7 @@ namespace StardewModdingAPI.Framework.Reflection return methodInfo; }); return method != null - ? new PrivateMethod(type, obj, method, isStatic) + ? new ReflectedMethod(type, obj, method, isStatic) : null; } -- cgit