summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Reflection
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-12-09 12:46:10 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-12-09 12:46:10 -0500
commit1398e591abc23a7af927cc7de1e8df512b6fc598 (patch)
tree28fbc26233e97cef258b3352206e8da9fa886304 /src/SMAPI/Framework/Reflection
parent5cc5f089b9645a60385ff293b5a7202f260bfc0f (diff)
downloadSMAPI-1398e591abc23a7af927cc7de1e8df512b6fc598.tar.gz
SMAPI-1398e591abc23a7af927cc7de1e8df512b6fc598.tar.bz2
SMAPI-1398e591abc23a7af927cc7de1e8df512b6fc598.zip
fix reflection API error with properties which don't have both get and set
Diffstat (limited to 'src/SMAPI/Framework/Reflection')
-rw-r--r--src/SMAPI/Framework/Reflection/PrivateProperty.cs20
1 files changed, 14 insertions, 6 deletions
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;
/// <summary>The underlying property getter.</summary>
- private readonly Func<TValue> GetterDelegate;
+ private readonly Func<TValue> GetMethod;
/// <summary>The underlying property setter.</summary>
- private readonly Action<TValue> SetterDelegate;
+ private readonly Action<TValue> SetMethod;
/*********
@@ -55,16 +55,21 @@ namespace StardewModdingAPI.Framework.Reflection
this.DisplayName = $"{parentType.FullName}::{property.Name}";
this.PropertyInfo = property;
- this.GetterDelegate = (Func<TValue>)Delegate.CreateDelegate(typeof(Func<TValue>), obj, this.PropertyInfo.GetMethod);
- this.SetterDelegate = (Action<TValue>)Delegate.CreateDelegate(typeof(Action<TValue>), obj, this.PropertyInfo.SetMethod);
+ 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 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
//// <param name="value">The value to set.</param>
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)
{