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 -------------------------- 1 file changed, 93 deletions(-) delete mode 100644 src/SMAPI/Framework/Reflection/PrivateField.cs (limited to 'src/SMAPI/Framework/Reflection/PrivateField.cs') 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); - } - } - } -} -- cgit