summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Reflection/PrivateField.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2016-12-09 12:25:53 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2016-12-09 12:25:53 -0500
commitcd0e5961d454e5861e2fd760388eb6920a1e2257 (patch)
treece7a206ba9992dfa07f78db46828e0f17f32f96d /src/StardewModdingAPI/Framework/Reflection/PrivateField.cs
parent25d2eb477729e929f3534b6f828c3fd96632e107 (diff)
downloadSMAPI-cd0e5961d454e5861e2fd760388eb6920a1e2257.tar.gz
SMAPI-cd0e5961d454e5861e2fd760388eb6920a1e2257.tar.bz2
SMAPI-cd0e5961d454e5861e2fd760388eb6920a1e2257.zip
add reflection API for mods (#185)
Diffstat (limited to 'src/StardewModdingAPI/Framework/Reflection/PrivateField.cs')
-rw-r--r--src/StardewModdingAPI/Framework/Reflection/PrivateField.cs94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/Reflection/PrivateField.cs b/src/StardewModdingAPI/Framework/Reflection/PrivateField.cs
new file mode 100644
index 00000000..6e7e3382
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/Reflection/PrivateField.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Reflection;
+using StardewModdingAPI.Reflection;
+
+namespace StardewModdingAPI.Framework.Reflection
+{
+ /// <summary>A private field obtained through reflection.</summary>
+ /// <typeparam name="TValue">The field value type.</typeparam>
+ internal class PrivateField<TValue> : IPrivateField<TValue>
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The type that has the field.</summary>
+ private readonly Type ParentType;
+
+ /// <summary>The object that has the instance field (if applicable).</summary>
+ private readonly object Parent;
+
+ /// <summary>The display name shown in error messages.</summary>
+ private string DisplayName => $"{this.ParentType.FullName}::{this.FieldInfo.Name}";
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The reflection metadata.</summary>
+ public FieldInfo FieldInfo { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="parentType">The type that has the field.</param>
+ /// <param name="obj">The object that has the instance field (if applicable).</param>
+ /// <param name="field">The reflection metadata.</param>
+ /// <param name="isStatic">Whether the field is static.</param>
+ /// <exception cref="ArgumentNullException">The <paramref name="parentType"/> or <paramref name="field"/> is null.</exception>
+ /// <exception cref="ArgumentException">The <paramref name="obj"/> is null for a non-static field, or not null for a static field.</exception>
+ 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;
+ }
+
+ /// <summary>Get the field value.</summary>
+ 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);
+ }
+ }
+
+ /// <summary>Set the field value.</summary>
+ //// <param name="value">The value to set.</param>
+ 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);
+ }
+ }
+ }
+}