diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-26 00:31:36 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-12-26 00:31:36 -0500 |
commit | 15d4b6310e3dd15c62f3faedbf1290b2db26fb59 (patch) | |
tree | 47d49a9c69628f0df1e688361f46bc5b46b3c0fd /src/SMAPI/Framework/Reflection/ReflectedProperty.cs | |
parent | 5cc5f089b9645a60385ff293b5a7202f260bfc0f (diff) | |
parent | f19cc3aac1a781bf2f2d20bc9577c2fe929b1e96 (diff) | |
download | SMAPI-15d4b6310e3dd15c62f3faedbf1290b2db26fb59.tar.gz SMAPI-15d4b6310e3dd15c62f3faedbf1290b2db26fb59.tar.bz2 SMAPI-15d4b6310e3dd15c62f3faedbf1290b2db26fb59.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Reflection/ReflectedProperty.cs')
-rw-r--r-- | src/SMAPI/Framework/Reflection/ReflectedProperty.cs | 105 |
1 files changed, 105 insertions, 0 deletions
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 +{ + /// <summary>A property obtained through reflection.</summary> + /// <typeparam name="TValue">The property value type.</typeparam> + internal class ReflectedProperty<TValue> : IPrivateProperty<TValue>, IReflectedProperty<TValue> + { + /********* + ** Properties + *********/ + /// <summary>The display name shown in error messages.</summary> + private readonly string DisplayName; + + /// <summary>The underlying property getter.</summary> + private readonly Func<TValue> GetMethod; + + /// <summary>The underlying property setter.</summary> + private readonly Action<TValue> SetMethod; + + + /********* + ** Accessors + *********/ + /// <summary>The reflection metadata.</summary> + public PropertyInfo PropertyInfo { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="parentType">The type that has the property.</param> + /// <param name="obj">The object that has the instance property (if applicable).</param> + /// <param name="property">The reflection metadata.</param> + /// <param name="isStatic">Whether the property is static.</param> + /// <exception cref="ArgumentNullException">The <paramref name="parentType"/> or <paramref name="property"/> is null.</exception> + /// <exception cref="ArgumentException">The <paramref name="obj"/> is null for a non-static property, or not null for a static property.</exception> + 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<TValue>)Delegate.CreateDelegate(typeof(Func<TValue>), obj, this.PropertyInfo.GetMethod); + if (this.PropertyInfo.SetMethod != null) + this.SetMethod = (Action<TValue>)Delegate.CreateDelegate(typeof(Action<TValue>), obj, this.PropertyInfo.SetMethod); + } + + /// <summary>Get the property value.</summary> + 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); + } + } + + /// <summary>Set the property value.</summary> + //// <param name="value">The value to set.</param> + 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); + } + } + } +} |