From 929dccb75a1405737975d76648e015a3e7c00177 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 7 Oct 2017 23:07:10 -0400 Subject: reorganise repo structure --- .../Framework/Reflection/PrivateMethod.cs | 99 ---------------------- 1 file changed, 99 deletions(-) delete mode 100644 src/StardewModdingAPI/Framework/Reflection/PrivateMethod.cs (limited to 'src/StardewModdingAPI/Framework/Reflection/PrivateMethod.cs') diff --git a/src/StardewModdingAPI/Framework/Reflection/PrivateMethod.cs b/src/StardewModdingAPI/Framework/Reflection/PrivateMethod.cs deleted file mode 100644 index ba2374f4..00000000 --- a/src/StardewModdingAPI/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 -- cgit