diff options
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r-- | src/SMAPI/Framework/Reflection/PrivateProperty.cs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/SMAPI/Framework/Reflection/PrivateProperty.cs b/src/SMAPI/Framework/Reflection/PrivateProperty.cs index 08204b7e..8a75d925 100644 --- a/src/SMAPI/Framework/Reflection/PrivateProperty.cs +++ b/src/SMAPI/Framework/Reflection/PrivateProperty.cs @@ -19,6 +19,9 @@ namespace StardewModdingAPI.Framework.Reflection /// <summary>The display name shown in error messages.</summary> private string DisplayName => $"{this.ParentType.FullName}::{this.PropertyInfo.Name}"; + private readonly Func<object, TValue> GetterDelegate; + private readonly Action<object, TValue> SetterDelegate; + /********* ** Accessors @@ -39,20 +42,17 @@ namespace StardewModdingAPI.Framework.Reflection /// <exception cref="ArgumentException">The <paramref name="obj"/> is null for a non-static field, or not null for a static field.</exception> public PrivateProperty(Type parentType, object obj, PropertyInfo property, bool isStatic) { - // validate - if (parentType == null) - throw new ArgumentNullException(nameof(parentType)); - if (property == null) - throw new ArgumentNullException(nameof(property)); 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."); - // save - this.ParentType = parentType; + this.ParentType = parentType ?? throw new ArgumentNullException(nameof(parentType)); this.Parent = obj; - this.PropertyInfo = property; + this.PropertyInfo = property ?? throw new ArgumentNullException(nameof(property)); + + this.GetterDelegate = (Func<object, TValue>)Delegate.CreateDelegate(typeof(Func<object, TValue>), this.PropertyInfo.GetMethod); + this.SetterDelegate = (Action<object, TValue>)Delegate.CreateDelegate(typeof(Action<object, TValue>), this.PropertyInfo.SetMethod); } /// <summary>Get the property value.</summary> @@ -60,7 +60,9 @@ namespace StardewModdingAPI.Framework.Reflection { try { - return (TValue)this.PropertyInfo.GetValue(this.Parent); + return this.GetterDelegate(this.Parent); + // Old version: Commented out in case of issues with new version + //return (TValue)this.PropertyInfo.GetValue(this.Parent); } catch (InvalidCastException) { @@ -78,7 +80,9 @@ namespace StardewModdingAPI.Framework.Reflection { try { - this.PropertyInfo.SetValue(this.Parent, value); + this.SetterDelegate(this.Parent, value); + // Old version: Commented out in case of issues with new version + //this.PropertyInfo.SetValue(this.Parent, value); } catch (InvalidCastException) { |