summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Framework/Reflection/PrivateProperty.cs30
1 files changed, 17 insertions, 13 deletions
diff --git a/src/SMAPI/Framework/Reflection/PrivateProperty.cs b/src/SMAPI/Framework/Reflection/PrivateProperty.cs
index 08204b7e..be346d71 100644
--- a/src/SMAPI/Framework/Reflection/PrivateProperty.cs
+++ b/src/SMAPI/Framework/Reflection/PrivateProperty.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Reflection;
namespace StardewModdingAPI.Framework.Reflection
@@ -10,14 +10,14 @@ namespace StardewModdingAPI.Framework.Reflection
/*********
** Properties
*********/
- /// <summary>The type that has the field.</summary>
- private readonly Type ParentType;
+ /// <summary>The display name shown in error messages.</summary>
+ private readonly string DisplayName;
- /// <summary>The object that has the instance field (if applicable).</summary>
- private readonly object Parent;
+ /// <summary>The underlying property getter.</summary>
+ private readonly Func<TValue> GetterDelegate;
- /// <summary>The display name shown in error messages.</summary>
- private string DisplayName => $"{this.ParentType.FullName}::{this.PropertyInfo.Name}";
+ /// <summary>The underlying property setter.</summary>
+ private readonly Action<TValue> SetterDelegate;
/*********
@@ -39,20 +39,24 @@ 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
+ // 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.");
- // save
- this.ParentType = parentType;
- this.Parent = obj;
+
+ 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);
}
/// <summary>Get the property value.</summary>
@@ -60,7 +64,7 @@ namespace StardewModdingAPI.Framework.Reflection
{
try
{
- return (TValue)this.PropertyInfo.GetValue(this.Parent);
+ return this.GetterDelegate();
}
catch (InvalidCastException)
{
@@ -78,7 +82,7 @@ namespace StardewModdingAPI.Framework.Reflection
{
try
{
- this.PropertyInfo.SetValue(this.Parent, value);
+ this.SetterDelegate(value);
}
catch (InvalidCastException)
{