summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Reflection
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Framework/Reflection')
-rw-r--r--src/SMAPI/Framework/Reflection/CacheEntry.cs12
-rw-r--r--src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs2
-rw-r--r--src/SMAPI/Framework/Reflection/ReflectedField.cs12
-rw-r--r--src/SMAPI/Framework/Reflection/ReflectedMethod.cs18
-rw-r--r--src/SMAPI/Framework/Reflection/ReflectedProperty.cs10
-rw-r--r--src/SMAPI/Framework/Reflection/Reflector.cs184
6 files changed, 101 insertions, 137 deletions
diff --git a/src/SMAPI/Framework/Reflection/CacheEntry.cs b/src/SMAPI/Framework/Reflection/CacheEntry.cs
index 6b18d204..27f48a1f 100644
--- a/src/SMAPI/Framework/Reflection/CacheEntry.cs
+++ b/src/SMAPI/Framework/Reflection/CacheEntry.cs
@@ -1,5 +1,4 @@
-#nullable disable
-
+using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace StardewModdingAPI.Framework.Reflection
@@ -11,21 +10,20 @@ namespace StardewModdingAPI.Framework.Reflection
** Accessors
*********/
/// <summary>Whether the lookup found a valid match.</summary>
- public bool IsValid { get; }
+ [MemberNotNullWhen(true, nameof(CacheEntry.MemberInfo))]
+ public bool IsValid => this.MemberInfo != null;
/// <summary>The reflection data for this member (or <c>null</c> if invalid).</summary>
- public MemberInfo MemberInfo { get; }
+ public MemberInfo? MemberInfo { get; }
/*********
** 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)
+ public CacheEntry(MemberInfo? memberInfo)
{
- this.IsValid = isValid;
this.MemberInfo = memberInfo;
}
}
diff --git a/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs b/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs
index 4c49e219..40adde8e 100644
--- a/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs
+++ b/src/SMAPI/Framework/Reflection/InterfaceProxyFactory.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System.Reflection;
using System.Reflection.Emit;
using Nanoray.Pintail;
diff --git a/src/SMAPI/Framework/Reflection/ReflectedField.cs b/src/SMAPI/Framework/Reflection/ReflectedField.cs
index 921876b9..a97ca3f0 100644
--- a/src/SMAPI/Framework/Reflection/ReflectedField.cs
+++ b/src/SMAPI/Framework/Reflection/ReflectedField.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Reflection;
@@ -15,8 +13,8 @@ namespace StardewModdingAPI.Framework.Reflection
/// <summary>The type that has the field.</summary>
private readonly Type ParentType;
- /// <summary>The object that has the instance field (if applicable).</summary>
- private readonly object Parent;
+ /// <summary>The object that has the instance field, or <c>null</c> for a static field.</summary>
+ private readonly object? Parent;
/// <summary>The display name shown in error messages.</summary>
private string DisplayName => $"{this.ParentType.FullName}::{this.FieldInfo.Name}";
@@ -34,12 +32,12 @@ namespace StardewModdingAPI.Framework.Reflection
*********/
/// <summary>Construct an instance.</summary>
/// <param name="parentType">The type that has the field.</param>
- /// <param name="obj">The object that has the instance field (if applicable).</param>
+ /// <param name="obj">The object that has the instance field, or <c>null</c> for a static field.</param>
/// <param name="field">The reflection metadata.</param>
/// <param name="isStatic">Whether the field is static.</param>
/// <exception cref="ArgumentNullException">The <paramref name="parentType"/> or <paramref name="field"/> is null.</exception>
/// <exception cref="ArgumentException">The <paramref name="obj"/> is null for a non-static field, or not null for a static field.</exception>
- public ReflectedField(Type parentType, object obj, FieldInfo field, bool isStatic)
+ public ReflectedField(Type parentType, object? obj, FieldInfo field, bool isStatic)
{
// validate
if (parentType == null)
@@ -62,7 +60,7 @@ namespace StardewModdingAPI.Framework.Reflection
{
try
{
- return (TValue)this.FieldInfo.GetValue(this.Parent);
+ return (TValue)this.FieldInfo.GetValue(this.Parent)!;
}
catch (InvalidCastException)
{
diff --git a/src/SMAPI/Framework/Reflection/ReflectedMethod.cs b/src/SMAPI/Framework/Reflection/ReflectedMethod.cs
index 50f89b40..a607141e 100644
--- a/src/SMAPI/Framework/Reflection/ReflectedMethod.cs
+++ b/src/SMAPI/Framework/Reflection/ReflectedMethod.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Reflection;
@@ -14,8 +12,8 @@ namespace StardewModdingAPI.Framework.Reflection
/// <summary>The type that has the method.</summary>
private readonly Type ParentType;
- /// <summary>The object that has the instance method (if applicable).</summary>
- private readonly object Parent;
+ /// <summary>The object that has the instance method, or <c>null</c> for a static method.</summary>
+ private readonly object? Parent;
/// <summary>The display name shown in error messages.</summary>
private string DisplayName => $"{this.ParentType.FullName}::{this.MethodInfo.Name}";
@@ -33,12 +31,12 @@ namespace StardewModdingAPI.Framework.Reflection
*********/
/// <summary>Construct an instance.</summary>
/// <param name="parentType">The type that has the method.</param>
- /// <param name="obj">The object that has the instance method(if applicable).</param>
+ /// <param name="obj">The object that has the instance method, or <c>null</c> for a static method.</param>
/// <param name="method">The reflection metadata.</param>
/// <param name="isStatic">Whether the method is static.</param>
/// <exception cref="ArgumentNullException">The <paramref name="parentType"/> or <paramref name="method"/> is null.</exception>
/// <exception cref="ArgumentException">The <paramref name="obj"/> is null for a non-static method, or not null for a static method.</exception>
- public ReflectedMethod(Type parentType, object obj, MethodInfo method, bool isStatic)
+ public ReflectedMethod(Type parentType, object? obj, MethodInfo method, bool isStatic)
{
// validate
if (parentType == null)
@@ -57,10 +55,10 @@ namespace StardewModdingAPI.Framework.Reflection
}
/// <inheritdoc />
- public TValue Invoke<TValue>(params object[] arguments)
+ public TValue Invoke<TValue>(params object?[] arguments)
{
// invoke method
- object result;
+ object? result;
try
{
result = this.MethodInfo.Invoke(this.Parent, arguments);
@@ -77,7 +75,7 @@ namespace StardewModdingAPI.Framework.Reflection
// cast return value
try
{
- return (TValue)result;
+ return (TValue)result!;
}
catch (InvalidCastException)
{
@@ -86,7 +84,7 @@ namespace StardewModdingAPI.Framework.Reflection
}
/// <inheritdoc />
- public void Invoke(params object[] arguments)
+ public void Invoke(params object?[] arguments)
{
// invoke method
try
diff --git a/src/SMAPI/Framework/Reflection/ReflectedProperty.cs b/src/SMAPI/Framework/Reflection/ReflectedProperty.cs
index a6d8c75c..72e701d1 100644
--- a/src/SMAPI/Framework/Reflection/ReflectedProperty.cs
+++ b/src/SMAPI/Framework/Reflection/ReflectedProperty.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Reflection;
@@ -16,10 +14,10 @@ namespace StardewModdingAPI.Framework.Reflection
private readonly string DisplayName;
/// <summary>The underlying property getter.</summary>
- private readonly Func<TValue> GetMethod;
+ private readonly Func<TValue>? GetMethod;
/// <summary>The underlying property setter.</summary>
- private readonly Action<TValue> SetMethod;
+ private readonly Action<TValue>? SetMethod;
/*********
@@ -34,12 +32,12 @@ namespace StardewModdingAPI.Framework.Reflection
*********/
/// <summary>Construct an instance.</summary>
/// <param name="parentType">The type that has the property.</param>
- /// <param name="obj">The object that has the instance property (if applicable).</param>
+ /// <param name="obj">The object that has the instance property, or <c>null</c> for a static property.</param>
/// <param name="property">The reflection metadata.</param>
/// <param name="isStatic">Whether the property is static.</param>
/// <exception cref="ArgumentNullException">The <paramref name="parentType"/> or <paramref name="property"/> is null.</exception>
/// <exception cref="ArgumentException">The <paramref name="obj"/> is null for a non-static property, or not null for a static property.</exception>
- public ReflectedProperty(Type parentType, object obj, PropertyInfo property, bool isStatic)
+ public ReflectedProperty(Type parentType, object? obj, PropertyInfo property, bool isStatic)
{
// validate input
if (parentType == null)
diff --git a/src/SMAPI/Framework/Reflection/Reflector.cs b/src/SMAPI/Framework/Reflection/Reflector.cs
index d5938c3f..79575c26 100644
--- a/src/SMAPI/Framework/Reflection/Reflector.cs
+++ b/src/SMAPI/Framework/Reflection/Reflector.cs
@@ -1,7 +1,4 @@
-#nullable disable
-
using System;
-using System.Linq;
using System.Reflection;
using System.Runtime.Caching;
@@ -15,7 +12,7 @@ namespace StardewModdingAPI.Framework.Reflection
** Fields
*********/
/// <summary>The cached fields and methods found via reflection.</summary>
- private readonly MemoryCache Cache = new(typeof(Reflector).FullName);
+ private readonly MemoryCache Cache = new(typeof(Reflector).FullName!);
/// <summary>The sliding cache expiration time.</summary>
private readonly TimeSpan SlidingCacheExpiry = TimeSpan.FromMinutes(5);
@@ -31,8 +28,9 @@ namespace StardewModdingAPI.Framework.Reflection
/// <typeparam name="TValue">The field type.</typeparam>
/// <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 field is not found.</param>
- /// <returns>Returns the field wrapper, or <c>null</c> if the field doesn't exist and <paramref name="required"/> is <c>false</c>.</returns>
+ /// <param name="required">Whether to throw an exception if the field isn't found. <strong>Due to limitations with nullable reference types, setting this to <c>false</c> will still mark the value non-nullable.</strong></param>
+ /// <returns>Returns the field wrapper, or <c>null</c> if <paramref name="required"/> is <c>false</c> and the field doesn't exist.</returns>
+ /// <exception cref="InvalidOperationException">The target field doesn't exist, and <paramref name="required"/> is true.</exception>
public IReflectedField<TValue> GetField<TValue>(object obj, string name, bool required = true)
{
// validate
@@ -40,24 +38,26 @@ namespace StardewModdingAPI.Framework.Reflection
throw new ArgumentNullException(nameof(obj), "Can't get a instance field from a null object.");
// get field from hierarchy
- IReflectedField<TValue> field = this.GetFieldFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+ IReflectedField<TValue>? field = this.GetFieldFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (required && field == null)
throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance field.");
- return field;
+ return field!;
}
/// <summary>Get a static field.</summary>
/// <typeparam name="TValue">The field type.</typeparam>
/// <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 field is not found.</param>
+ /// <param name="required">Whether to throw an exception if the field isn't found. <strong>Due to limitations with nullable reference types, setting this to <c>false</c> will still mark the value non-nullable.</strong></param>
+ /// <returns>Returns the field wrapper, or <c>null</c> if <paramref name="required"/> is <c>false</c> and the field doesn't exist.</returns>
+ /// <exception cref="InvalidOperationException">The target field doesn't exist, and <paramref name="required"/> is true.</exception>
public IReflectedField<TValue> GetField<TValue>(Type type, string name, bool required = true)
{
// get field from hierarchy
- IReflectedField<TValue> field = this.GetFieldFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public);
+ IReflectedField<TValue>? field = this.GetFieldFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public);
if (required && field == null)
throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static field.");
- return field;
+ return field!;
}
/****
@@ -67,7 +67,9 @@ namespace StardewModdingAPI.Framework.Reflection
/// <typeparam name="TValue">The property type.</typeparam>
/// <param name="obj">The object which has the property.</param>
/// <param name="name">The property name.</param>
- /// <param name="required">Whether to throw an exception if the property is not found.</param>
+ /// <param name="required">Whether to throw an exception if the property isn't found. <strong>Due to limitations with nullable reference types, setting this to <c>false</c> will still mark the value non-nullable.</strong></param>
+ /// <returns>Returns the property wrapper, or <c>null</c> if <paramref name="required"/> is <c>false</c> and the property doesn't exist.</returns>
+ /// <exception cref="InvalidOperationException">The target property doesn't exist, and <paramref name="required"/> is true.</exception>
public IReflectedProperty<TValue> GetProperty<TValue>(object obj, string name, bool required = true)
{
// validate
@@ -75,24 +77,26 @@ namespace StardewModdingAPI.Framework.Reflection
throw new ArgumentNullException(nameof(obj), "Can't get a instance property from a null object.");
// get property from hierarchy
- IReflectedProperty<TValue> property = this.GetPropertyFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+ IReflectedProperty<TValue>? property = this.GetPropertyFromHierarchy<TValue>(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (required && property == null)
throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance property.");
- return property;
+ return property!;
}
/// <summary>Get a static property.</summary>
/// <typeparam name="TValue">The property type.</typeparam>
/// <param name="type">The type which has the property.</param>
/// <param name="name">The property name.</param>
- /// <param name="required">Whether to throw an exception if the property is not found.</param>
+ /// <param name="required">Whether to throw an exception if the property isn't found. <strong>Due to limitations with nullable reference types, setting this to <c>false</c> will still mark the value non-nullable.</strong></param>
+ /// <returns>Returns the property wrapper, or <c>null</c> if <paramref name="required"/> is <c>false</c> and the property doesn't exist.</returns>
+ /// <exception cref="InvalidOperationException">The target property doesn't exist, and <paramref name="required"/> is true.</exception>
public IReflectedProperty<TValue> GetProperty<TValue>(Type type, string name, bool required = true)
{
// get field from hierarchy
- IReflectedProperty<TValue> property = this.GetPropertyFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
+ IReflectedProperty<TValue>? property = this.GetPropertyFromHierarchy<TValue>(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if (required && property == null)
throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static property.");
- return property;
+ return property!;
}
/****
@@ -100,8 +104,10 @@ namespace StardewModdingAPI.Framework.Reflection
****/
/// <summary>Get a instance method.</summary>
/// <param name="obj">The object which has the method.</param>
- /// <param name="name">The field name.</param>
- /// <param name="required">Whether to throw an exception if the field is not found.</param>
+ /// <param name="name">The method name.</param>
+ /// <param name="required">Whether to throw an exception if the method isn't found. <strong>Due to limitations with nullable reference types, setting this to <c>false</c> will still mark the value non-nullable.</strong></param>
+ /// <returns>Returns the method wrapper, or <c>null</c> if <paramref name="required"/> is <c>false</c> and the method doesn't exist.</returns>
+ /// <exception cref="InvalidOperationException">The target method doesn't exist, and <paramref name="required"/> is true.</exception>
public IReflectedMethod GetMethod(object obj, string name, bool required = true)
{
// validate
@@ -109,58 +115,25 @@ namespace StardewModdingAPI.Framework.Reflection
throw new ArgumentNullException(nameof(obj), "Can't get a instance method from a null object.");
// get method from hierarchy
- IReflectedMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+ IReflectedMethod? method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (required && method == null)
throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance method.");
- return method;
+ return method!;
}
/// <summary>Get a static method.</summary>
/// <param name="type">The type which has the method.</param>
- /// <param name="name">The field name.</param>
- /// <param name="required">Whether to throw an exception if the field is not found.</param>
+ /// <param name="name">The method name.</param>
+ /// <param name="required">Whether to throw an exception if the method isn't found. <strong>Due to limitations with nullable reference types, setting this to <c>false</c> will still mark the value non-nullable.</strong></param>
+ /// <returns>Returns the method wrapper, or <c>null</c> if <paramref name="required"/> is <c>false</c> and the method doesn't exist.</returns>
+ /// <exception cref="InvalidOperationException">The target method doesn't exist, and <paramref name="required"/> is true.</exception>
public IReflectedMethod GetMethod(Type type, string name, bool required = true)
{
// get method from hierarchy
- IReflectedMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
+ IReflectedMethod? method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if (required && method == null)
throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static method.");
- return method;
- }
-
- /****
- ** Methods by signature
- ****/
- /// <summary>Get a instance method.</summary>
- /// <param name="obj">The object which has the method.</param>
- /// <param name="name">The field name.</param>
- /// <param name="argumentTypes">The argument types of the method signature to find.</param>
- /// <param name="required">Whether to throw an exception if the field is not found.</param>
- public IReflectedMethod GetMethod(object obj, string name, Type[] argumentTypes, bool required = true)
- {
- // validate parent
- if (obj == null)
- throw new ArgumentNullException(nameof(obj), "Can't get a instance method from a null object.");
-
- // get method from hierarchy
- ReflectedMethod method = this.GetMethodFromHierarchy(obj.GetType(), obj, name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, argumentTypes);
- if (required && method == null)
- throw new InvalidOperationException($"The {obj.GetType().FullName} object doesn't have a '{name}' instance method with that signature.");
- return method;
- }
-
- /// <summary>Get a static method.</summary>
- /// <param name="type">The type which has the method.</param>
- /// <param name="name">The field name.</param>
- /// <param name="argumentTypes">The argument types of the method signature to find.</param>
- /// <param name="required">Whether to throw an exception if the field is not found.</param>
- public IReflectedMethod GetMethod(Type type, string name, Type[] argumentTypes, bool required = true)
- {
- // get field from hierarchy
- ReflectedMethod method = this.GetMethodFromHierarchy(type, null, name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, argumentTypes);
- if (required && method == null)
- throw new InvalidOperationException($"The {type.FullName} object doesn't have a '{name}' static method with that signature.");
- return method;
+ return method!;
}
@@ -170,18 +143,25 @@ namespace StardewModdingAPI.Framework.Reflection
/// <summary>Get a field from the type hierarchy.</summary>
/// <typeparam name="TValue">The expected field type.</typeparam>
/// <param name="type">The type which has the field.</param>
- /// <param name="obj">The object which has the field.</param>
+ /// <param name="obj">The object which has the field, or <c>null</c> for a static field.</param>
/// <param name="name">The field name.</param>
/// <param name="bindingFlags">The reflection binding which flags which indicates what type of field to find.</param>
- private IReflectedField<TValue> GetFieldFromHierarchy<TValue>(Type type, object obj, string name, BindingFlags bindingFlags)
+ private IReflectedField<TValue>? GetFieldFromHierarchy<TValue>(Type type, object? obj, string name, BindingFlags bindingFlags)
{
bool isStatic = bindingFlags.HasFlag(BindingFlags.Static);
- FieldInfo field = this.GetCached<FieldInfo>($"field::{isStatic}::{type.FullName}::{name}", () =>
+ FieldInfo? field = this.GetCached($"field::{isStatic}::{type.FullName}::{name}", () =>
{
- FieldInfo fieldInfo = null;
- for (; type != null && fieldInfo == null; type = type.BaseType)
- fieldInfo = type.GetField(name, bindingFlags);
- return fieldInfo;
+ for (Type? curType = type; curType != null; curType = curType.BaseType)
+ {
+ FieldInfo? fieldInfo = curType.GetField(name, bindingFlags);
+ if (fieldInfo != null)
+ {
+ type = curType;
+ return fieldInfo;
+ }
+ }
+
+ return null;
});
return field != null
@@ -192,18 +172,25 @@ namespace StardewModdingAPI.Framework.Reflection
/// <summary>Get a property from the type hierarchy.</summary>
/// <typeparam name="TValue">The expected property type.</typeparam>
/// <param name="type">The type which has the property.</param>
- /// <param name="obj">The object which has the property.</param>
+ /// <param name="obj">The object which has the property, or <c>null</c> for a static property.</param>
/// <param name="name">The property name.</param>
/// <param name="bindingFlags">The reflection binding which flags which indicates what type of property to find.</param>
- private IReflectedProperty<TValue> GetPropertyFromHierarchy<TValue>(Type type, object obj, string name, BindingFlags bindingFlags)
+ private IReflectedProperty<TValue>? GetPropertyFromHierarchy<TValue>(Type type, object? obj, string name, BindingFlags bindingFlags)
{
bool isStatic = bindingFlags.HasFlag(BindingFlags.Static);
- PropertyInfo property = this.GetCached<PropertyInfo>($"property::{isStatic}::{type.FullName}::{name}", () =>
+ PropertyInfo? property = this.GetCached<PropertyInfo>($"property::{isStatic}::{type.FullName}::{name}", () =>
{
- PropertyInfo propertyInfo = null;
- for (; type != null && propertyInfo == null; type = type.BaseType)
- propertyInfo = type.GetProperty(name, bindingFlags);
- return propertyInfo;
+ for (Type? curType = type; curType != null; curType = curType.BaseType)
+ {
+ PropertyInfo? propertyInfo = curType.GetProperty(name, bindingFlags);
+ if (propertyInfo != null)
+ {
+ type = curType;
+ return propertyInfo;
+ }
+ }
+
+ return null;
});
return property != null
@@ -213,18 +200,25 @@ namespace StardewModdingAPI.Framework.Reflection
/// <summary>Get a method from the type hierarchy.</summary>
/// <param name="type">The type which has the method.</param>
- /// <param name="obj">The object which has the method.</param>
+ /// <param name="obj">The object which has the method, or <c>null</c> for a static method.</param>
/// <param name="name">The method name.</param>
/// <param name="bindingFlags">The reflection binding which flags which indicates what type of method to find.</param>
- private IReflectedMethod GetMethodFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags)
+ private IReflectedMethod? GetMethodFromHierarchy(Type type, object? obj, string name, BindingFlags bindingFlags)
{
bool isStatic = bindingFlags.HasFlag(BindingFlags.Static);
- MethodInfo method = this.GetCached($"method::{isStatic}::{type.FullName}::{name}", () =>
+ MethodInfo? method = this.GetCached($"method::{isStatic}::{type.FullName}::{name}", () =>
{
- MethodInfo methodInfo = null;
- for (; type != null && methodInfo == null; type = type.BaseType)
- methodInfo = type.GetMethod(name, bindingFlags);
- return methodInfo;
+ for (Type? curType = type; curType != null; curType = curType.BaseType)
+ {
+ MethodInfo? methodInfo = curType.GetMethod(name, bindingFlags);
+ if (methodInfo != null)
+ {
+ type = curType;
+ return methodInfo;
+ }
+ }
+
+ return null;
});
return method != null
@@ -232,32 +226,12 @@ namespace StardewModdingAPI.Framework.Reflection
: null;
}
- /// <summary>Get a method from the type hierarchy.</summary>
- /// <param name="type">The type which has the method.</param>
- /// <param name="obj">The object which has the method.</param>
- /// <param name="name">The method name.</param>
- /// <param name="bindingFlags">The reflection binding which flags which indicates what type of method to find.</param>
- /// <param name="argumentTypes">The argument types of the method signature to find.</param>
- private ReflectedMethod GetMethodFromHierarchy(Type type, object obj, string name, BindingFlags bindingFlags, Type[] argumentTypes)
- {
- bool isStatic = bindingFlags.HasFlag(BindingFlags.Static);
- MethodInfo method = this.GetCached($"method::{isStatic}::{type.FullName}::{name}({string.Join(",", argumentTypes.Select(p => p.FullName))})", () =>
- {
- MethodInfo methodInfo = null;
- for (; type != null && methodInfo == null; type = type.BaseType)
- methodInfo = type.GetMethod(name, bindingFlags, null, argumentTypes, null);
- return methodInfo;
- });
- return method != null
- ? new ReflectedMethod(type, obj, method, isStatic)
- : null;
- }
-
/// <summary>Get a method or field through the cache.</summary>
/// <typeparam name="TMemberInfo">The expected <see cref="MemberInfo"/> type.</typeparam>
/// <param name="key">The cache key.</param>
/// <param name="fetch">Fetches a new value to cache.</param>
- private TMemberInfo GetCached<TMemberInfo>(string key, Func<TMemberInfo> fetch) where TMemberInfo : MemberInfo
+ private TMemberInfo? GetCached<TMemberInfo>(string key, Func<TMemberInfo?> fetch)
+ where TMemberInfo : MemberInfo
{
// get from cache
if (this.Cache.Contains(key))
@@ -269,8 +243,8 @@ namespace StardewModdingAPI.Framework.Reflection
}
// fetch & cache new value
- TMemberInfo result = fetch();
- CacheEntry cacheEntry = new(result != null, result);
+ TMemberInfo? result = fetch();
+ CacheEntry cacheEntry = new(result);
this.Cache.Add(key, cacheEntry, new CacheItemPolicy { SlidingExpiration = this.SlidingCacheExpiry });
return result;
}