PrivateProperty.cs ~ Use delegates for performance
This commit is contained in:
parent
749ebb912b
commit
ded647aad4
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue