diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-24 20:28:59 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-24 20:28:59 -0400 |
commit | fd2b9c72a84e6227bf489a6f044d843e50b6c769 (patch) | |
tree | 72cb9884e981cf8ef587c584dc09ab9805cae47e /src/SMAPI | |
parent | 749ebb912bc59fceda2f14d7e330cd9edeff19ff (diff) | |
parent | f6a86e584976c87f1f678a226f8eafe6a8b9860c (diff) | |
download | SMAPI-fd2b9c72a84e6227bf489a6f044d843e50b6c769.tar.gz SMAPI-fd2b9c72a84e6227bf489a6f044d843e50b6c769.tar.bz2 SMAPI-fd2b9c72a84e6227bf489a6f044d843e50b6c769.zip |
Merge branch 'optimise-property-reflection' into develop
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Framework/Reflection/PrivateProperty.cs | 30 |
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) { |