diff options
Diffstat (limited to 'src/SMAPI/Framework/Reflection')
-rw-r--r-- | src/SMAPI/Framework/Reflection/ReflectedField.cs | 16 | ||||
-rw-r--r-- | src/SMAPI/Framework/Reflection/ReflectedMethod.cs | 18 | ||||
-rw-r--r-- | src/SMAPI/Framework/Reflection/ReflectedProperty.cs | 18 |
3 files changed, 23 insertions, 29 deletions
diff --git a/src/SMAPI/Framework/Reflection/ReflectedField.cs b/src/SMAPI/Framework/Reflection/ReflectedField.cs index 921876b9..197a246e 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) @@ -58,11 +56,11 @@ namespace StardewModdingAPI.Framework.Reflection } /// <inheritdoc /> - public TValue GetValue() + public TValue? GetValue() { try { - return (TValue)this.FieldInfo.GetValue(this.Parent); + return (TValue?)this.FieldInfo.GetValue(this.Parent); } catch (InvalidCastException) { @@ -75,7 +73,7 @@ namespace StardewModdingAPI.Framework.Reflection } /// <inheritdoc /> - public void SetValue(TValue value) + public void SetValue(TValue? value) { try { diff --git a/src/SMAPI/Framework/Reflection/ReflectedMethod.cs b/src/SMAPI/Framework/Reflection/ReflectedMethod.cs index 50f89b40..c245e843 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..638953a3 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) @@ -58,13 +56,13 @@ namespace StardewModdingAPI.Framework.Reflection this.PropertyInfo = property; if (this.PropertyInfo.GetMethod != null) - this.GetMethod = (Func<TValue>)Delegate.CreateDelegate(typeof(Func<TValue>), obj, this.PropertyInfo.GetMethod); + this.GetMethod = (Func<TValue?>)Delegate.CreateDelegate(typeof(Func<TValue?>), obj, this.PropertyInfo.GetMethod); if (this.PropertyInfo.SetMethod != null) - this.SetMethod = (Action<TValue>)Delegate.CreateDelegate(typeof(Action<TValue>), obj, this.PropertyInfo.SetMethod); + this.SetMethod = (Action<TValue?>)Delegate.CreateDelegate(typeof(Action<TValue?>), obj, this.PropertyInfo.SetMethod); } /// <inheritdoc /> - public TValue GetValue() + public TValue? GetValue() { if (this.GetMethod == null) throw new InvalidOperationException($"The {this.DisplayName} property has no get method."); @@ -84,7 +82,7 @@ namespace StardewModdingAPI.Framework.Reflection } /// <inheritdoc /> - public void SetValue(TValue value) + public void SetValue(TValue? value) { if (this.SetMethod == null) throw new InvalidOperationException($"The {this.DisplayName} property has no set method."); |