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) --- .../Framework/Reflection/ReflectedProperty.cs | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/SMAPI/Framework/Reflection/ReflectedProperty.cs (limited to 'src/SMAPI/Framework/Reflection/ReflectedProperty.cs') 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); + } + } + } +} -- cgit