From 1398e591abc23a7af927cc7de1e8df512b6fc598 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 9 Dec 2017 12:46:10 -0500 Subject: fix reflection API error with properties which don't have both get and set --- src/SMAPI/Framework/Reflection/PrivateProperty.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/SMAPI/Framework/Reflection/PrivateProperty.cs') diff --git a/src/SMAPI/Framework/Reflection/PrivateProperty.cs b/src/SMAPI/Framework/Reflection/PrivateProperty.cs index be346d71..c81f99c7 100644 --- a/src/SMAPI/Framework/Reflection/PrivateProperty.cs +++ b/src/SMAPI/Framework/Reflection/PrivateProperty.cs @@ -14,10 +14,10 @@ namespace StardewModdingAPI.Framework.Reflection private readonly string DisplayName; /// The underlying property getter. - private readonly Func GetterDelegate; + private readonly Func GetMethod; /// The underlying property setter. - private readonly Action SetterDelegate; + private readonly Action SetMethod; /********* @@ -55,16 +55,21 @@ namespace StardewModdingAPI.Framework.Reflection this.DisplayName = $"{parentType.FullName}::{property.Name}"; this.PropertyInfo = property; - this.GetterDelegate = (Func)Delegate.CreateDelegate(typeof(Func), obj, this.PropertyInfo.GetMethod); - this.SetterDelegate = (Action)Delegate.CreateDelegate(typeof(Action), obj, this.PropertyInfo.SetMethod); + 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 private {this.DisplayName} property has no get method."); + try { - return this.GetterDelegate(); + return this.GetMethod(); } catch (InvalidCastException) { @@ -80,9 +85,12 @@ namespace StardewModdingAPI.Framework.Reflection //// The value to set. public void SetValue(TValue value) { + if (this.SetMethod == null) + throw new InvalidOperationException($"The private {this.DisplayName} property has no set method."); + try { - this.SetterDelegate(value); + this.SetMethod(value); } catch (InvalidCastException) { -- cgit