format code (#193)
This commit is contained in:
parent
cd93d59eaf
commit
d9e87399bf
|
@ -0,0 +1,30 @@
|
|||
using System.Reflection;
|
||||
|
||||
namespace StardewModdingAPI.Framework.Reflection
|
||||
{
|
||||
/// <summary>A cached member reflection result.</summary>
|
||||
internal struct CacheEntry
|
||||
{
|
||||
/*********
|
||||
** Accessors
|
||||
*********/
|
||||
/// <summary>Whether the lookup found a valid match.</summary>
|
||||
public bool IsValid;
|
||||
|
||||
/// <summary>The reflection data for this member (or <c>null</c> if invalid).</summary>
|
||||
public MemberInfo MemberInfo;
|
||||
|
||||
|
||||
/*********
|
||||
** Public methods
|
||||
*********/
|
||||
/// <summary>Construct an instance.</summary>
|
||||
/// <param name="isValid">Whether the lookup found a valid match.</param>
|
||||
/// <param name="memberInfo">The reflection data for this member (or <c>null</c> if invalid).</param>
|
||||
public CacheEntry(bool isValid, MemberInfo memberInfo)
|
||||
{
|
||||
this.IsValid = isValid;
|
||||
this.MemberInfo = memberInfo;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,16 +12,6 @@ namespace StardewModdingAPI.Framework.Reflection
|
|||
/*********
|
||||
** Properties
|
||||
*********/
|
||||
/// <summary>MemberInfo wrapper for tracking validity.</summary>
|
||||
internal struct CacheEntry
|
||||
{
|
||||
/// <summary>Is this member valid. Used to avoid unecessary lookups.</summary>
|
||||
public bool IsValid;
|
||||
|
||||
/// <summary>The reflection data for this member. This will be null if IsValid is false.</summary>
|
||||
public MemberInfo MemberInfo;
|
||||
}
|
||||
|
||||
/// <summary>The cached fields and methods found via reflection.</summary>
|
||||
private readonly MemoryCache Cache = new MemoryCache(typeof(ReflectionHelper).FullName);
|
||||
|
||||
|
@ -77,7 +67,7 @@ namespace StardewModdingAPI.Framework.Reflection
|
|||
/// <param name="obj">The object which has the field.</param>
|
||||
/// <param name="name">The field name.</param>
|
||||
/// <param name="required">Whether to throw an exception if the private field is not found.</param>
|
||||
/// <returns>The value of the field or the default value of the type if the field is not found.</returns>
|
||||
/// <returns>Returns the field value, or the default value for <typeparamref name="TValue"/> if the field wasn't found and <paramref name="required"/> is false.</returns>
|
||||
/// <remarks>
|
||||
/// This is a shortcut for <see cref="GetPrivateField{TValue}(object,string,bool)"/> followed by <see cref="IPrivateField{TValue}.GetValue"/>.
|
||||
/// When <paramref name="required" /> is false, this will return the default value if reflection fails. If you need to check whether the field exists, use <see cref="GetPrivateField{TValue}(object,string,bool)" /> instead.
|
||||
|
@ -85,7 +75,7 @@ namespace StardewModdingAPI.Framework.Reflection
|
|||
public TValue GetPrivateValue<TValue>(object obj, string name, bool required = true)
|
||||
{
|
||||
IPrivateField<TValue> field = this.GetPrivateField<TValue>(obj, name, required);
|
||||
return (field != null)
|
||||
return field != null
|
||||
? field.GetValue()
|
||||
: default(TValue);
|
||||
}
|
||||
|
@ -95,7 +85,7 @@ namespace StardewModdingAPI.Framework.Reflection
|
|||
/// <param name="type">The type which has the field.</param>
|
||||
/// <param name="name">The field name.</param>
|
||||
/// <param name="required">Whether to throw an exception if the private field is not found.</param>
|
||||
/// <returns>The value of the field or the default value of the type if the field is not found.</returns>
|
||||
/// <returns>Returns the field value, or the default value for <typeparamref name="TValue"/> if the field wasn't found and <paramref name="required"/> is false.</returns>
|
||||
/// <remarks>
|
||||
/// This is a shortcut for <see cref="GetPrivateField{TValue}(Type,string,bool)"/> followed by <see cref="IPrivateField{TValue}.GetValue"/>.
|
||||
/// When <paramref name="required" /> is false, this will return the default value if reflection fails. If you need to check whether the field exists, use <see cref="GetPrivateField{TValue}(Type,string,bool)" /> instead.
|
||||
|
@ -103,7 +93,7 @@ namespace StardewModdingAPI.Framework.Reflection
|
|||
public TValue GetPrivateValue<TValue>(Type type, string name, bool required = true)
|
||||
{
|
||||
IPrivateField<TValue> field = this.GetPrivateField<TValue>(type, name, required);
|
||||
return (field != null)
|
||||
return field != null
|
||||
? field.GetValue()
|
||||
: default(TValue);
|
||||
}
|
||||
|
@ -254,21 +244,16 @@ namespace StardewModdingAPI.Framework.Reflection
|
|||
if (this.Cache.Contains(key))
|
||||
{
|
||||
CacheEntry entry = (CacheEntry)this.Cache[key];
|
||||
return entry.IsValid
|
||||
? (TMemberInfo)entry.MemberInfo
|
||||
return entry.IsValid
|
||||
? (TMemberInfo)entry.MemberInfo
|
||||
: default(TMemberInfo);
|
||||
}
|
||||
|
||||
// fetch & cache new value, marking if it's valid for future lookups.
|
||||
// fetch & cache new value
|
||||
TMemberInfo result = fetch();
|
||||
CacheEntry cacheEntry = new CacheEntry()
|
||||
{
|
||||
IsValid = (result != null),
|
||||
MemberInfo = result
|
||||
};
|
||||
|
||||
CacheEntry cacheEntry = new CacheEntry(result != null, result);
|
||||
this.Cache.Add(key, cacheEntry, new CacheItemPolicy { SlidingExpiration = this.SlidingCacheExpiry });
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -162,6 +162,7 @@
|
|||
<Compile Include="Framework\DeprecationManager.cs" />
|
||||
<Compile Include="Framework\InternalExtensions.cs" />
|
||||
<Compile Include="Framework\ModAssemblyLoader.cs" />
|
||||
<Compile Include="Framework\Reflection\CacheEntry.cs" />
|
||||
<Compile Include="Framework\Reflection\PrivateField.cs" />
|
||||
<Compile Include="Framework\Reflection\PrivateMethod.cs" />
|
||||
<Compile Include="Framework\Reflection\ReflectionHelper.cs" />
|
||||
|
|
Loading…
Reference in New Issue