From 57ed5064a471b7b71219627272d3906a253f35ac Mon Sep 17 00:00:00 2001 From: Andreas Pardeike Date: Thu, 19 Dec 2024 20:20:02 +0100 Subject: [PATCH] housekeeping, fixes docs error, net9 for docs, better api docs landing page, net9 locking in filelog --- Harmony/Documentation/Documentation.csproj | 6 +- Harmony/Documentation/GlobalSuppressions.cs | 1 + Harmony/Documentation/api/index.md | 61 ++++++++++++++++++- .../patching-transpiler-codematcher.cs | 6 +- Harmony/Extras/FastAccess.cs | 10 +-- Harmony/Extras/MethodInvoker.cs | 2 +- Harmony/GlobalSuppressions.cs | 1 + Harmony/Tools/AccessTools.cs | 11 ++-- Harmony/Tools/FileLog.cs | 7 +++ Harmony/Tools/Tools.cs | 6 +- HarmonyTests/GlobalSuppressions.cs | 2 + HarmonyTests/Patching/Assets/ArgumentCases.cs | 7 ++- HarmonyTests/Patching/FinalizerPatches.cs | 4 +- HarmonyTests/Patching/Specials.cs | 2 +- HarmonyTests/Tools/TestAccessTools.cs | 58 +++++++++--------- HarmonyTests/Tools/TestFieldRefAccess.cs | 4 +- 16 files changed, 132 insertions(+), 56 deletions(-) diff --git a/Harmony/Documentation/Documentation.csproj b/Harmony/Documentation/Documentation.csproj index 4bd654cb..1f788a03 100644 --- a/Harmony/Documentation/Documentation.csproj +++ b/Harmony/Documentation/Documentation.csproj @@ -1,7 +1,7 @@ - net35;net452;net472;net48;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 + net35;net452;net472;net48;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0 preview false obj @@ -19,8 +19,8 @@ - runtime; build; native; contentfiles; analyzers; buildtransitive - all + runtime; build; native; contentfiles; analyzers; buildtransitive + all diff --git a/Harmony/Documentation/GlobalSuppressions.cs b/Harmony/Documentation/GlobalSuppressions.cs index 8bf71a0e..431244ee 100644 --- a/Harmony/Documentation/GlobalSuppressions.cs +++ b/Harmony/Documentation/GlobalSuppressions.cs @@ -22,3 +22,4 @@ [assembly: SuppressMessage("", "CA1016")] [assembly: SuppressMessage("", "CS0649")] [assembly: SuppressMessage("", "IDE0251")] +[assembly: SuppressMessage("", "IDE0130")] diff --git a/Harmony/Documentation/api/index.md b/Harmony/Documentation/api/index.md index e06f2ee6..96e6e1f6 100644 --- a/Harmony/Documentation/api/index.md +++ b/Harmony/Documentation/api/index.md @@ -1 +1,60 @@ -# Harmony API \ No newline at end of file +# Harmony API + +Use the navigation to the left to drill down into the Harmony API documentation. + +Here is a selected list of classes: + +### Basics + +- [Harmony](HarmonyLib.html#Harmony) +- [PatchClassProcessor](HarmonyLib.html#PatchClassProcessor) +- [PatchProcessor](HarmonyLib.html#PatchProcessor) +- [HarmonyPatch](HarmonyLib.HarmonyPatch.html) +- [PatchInfo](HarmonyLib.PatchInfo.html) +- [Patches](HarmonyLib.Patches.html) +- [Patch](HarmonyLib.Patch.html) + +### Targeting + +- [HarmonyMethod](HarmonyLib.HarmonyMethod.html) +- [HarmonyMethodExtensions](HarmonyLib.HarmonyMethodExtensions.html) +- [MethodType](HarmonyLib.MethodType.html) + +### Attributes + +- [HarmonyAttribute](HarmonyLib.HarmonyAttribute.html) +- [HarmonyPrefix](HarmonyLib.HarmonyPrefix.html) +- [HarmonyPostfix](HarmonyLib.HarmonyPostfix.html) +- [HarmonyTranspiler](HarmonyLib.HarmonyTranspiler.html) +- [HarmonyFinalizer](HarmonyLib.HarmonyFinalizer.html) +- [HarmonyDebug](HarmonyLib.HarmonyDebug.html) + +### Additional patch class annotations + +- [HarmonyTargetMethod](HarmonyLib.HarmonyTargetMethod.html) +- [HarmonyTargetMethods](HarmonyLib.HarmonyTargetMethods.html) +- [HarmonyPrepare](HarmonyLib.HarmonyPrepare.html) +- [HarmonyCleanup](HarmonyLib.HarmonyCleanup.html) +- [HarmonyArgument](HarmonyLib.HarmonyArgument.html) + +### Transpiling + +- [Transpilers](HarmonyLib.Transpilers.html) +- [CodeInstruction](HarmonyLib.CodeInstruction.html) +- [CodeInstructionExtensions](HarmonyLib.CodeInstructionExtensions.html) +- [CodeInstructionsExtensions](HarmonyLib.CodeInstructionsExtensions.html) +- [CodeMatcher](HarmonyLib.CodeMatcher.html) +- [CodeMatch](HarmonyLib.CodeMatch.html) +- [Code](HarmonyLib.Code.html) + + +### Helper classes + +- [AccessTools](HarmonyLib.AccessTools.html) +- [AccessToolsExtensions](HarmonyLib.AccessToolsExtensions.html) +- [SymbolExtensions](HarmonyLib.SymbolExtensions.html) +- [FastAccess](HarmonyLib.FastAccess.html) +- [FileLog](HarmonyLib.FileLog.html) +- [Traverse](HarmonyLib.Traverse.html) +- [GeneralExtensions](HarmonyLib.GeneralExtensions.html) +- [HarmonyDelegate](HarmonyLib.HarmonyDelegate.html) diff --git a/Harmony/Documentation/examples/patching-transpiler-codematcher.cs b/Harmony/Documentation/examples/patching-transpiler-codematcher.cs index 7f6aaec7..6d382f45 100644 --- a/Harmony/Documentation/examples/patching-transpiler-codematcher.cs +++ b/Harmony/Documentation/examples/patching-transpiler-codematcher.cs @@ -29,7 +29,7 @@ static IEnumerable Transpiler(IEnumerable inst var codeMatcher = new CodeMatcher(instructions /*, ILGenerator generator*/); codeMatcher.MatchStartForward( - CodeMatch.Calls(() => default(DamageHandler).Kill(default(Player))) + CodeMatch.Calls(() => default(DamageHandler).Kill(default)) ) .ThrowIfInvalid("Could not find call to DamageHandler.Kill") .RemoveInstruction() @@ -45,7 +45,7 @@ static IEnumerable Transpiler(IEnumerable inst [HarmonyPatch] public static class DamageHandler_Apply_Patch_Alternative { - static IEnumerable TargetMethods() => new List(); + static IEnumerable TargetMethods() => []; static void MyDeathHandler(DamageHandler handler, Player player) { } @@ -55,7 +55,7 @@ static IEnumerable Transpiler(IEnumerable inst // codeMatcher.ThrowIfNotMatchForward("Could not find call to DamageHandler.Kill", - CodeMatch.Calls(() => default(DamageHandler).Kill(default(Player))) + CodeMatch.Calls(() => default(DamageHandler).Kill(default)) ) .RemoveInstruction() .InsertAndAdvance( diff --git a/Harmony/Extras/FastAccess.cs b/Harmony/Extras/FastAccess.cs index e58d4bb3..f048683f 100644 --- a/Harmony/Extras/FastAccess.cs +++ b/Harmony/Extras/FastAccess.cs @@ -55,7 +55,7 @@ public static InstantiationHandler CreateInstantiationHandler() var generator = dynamicMethod.GetILGenerator(); generator.Emit(OpCodes.Newobj, constructorInfo); generator.Emit(OpCodes.Ret); - return (InstantiationHandler)dynamicMethod.Generate().CreateDelegate(typeof(InstantiationHandler)); + return dynamicMethod.Generate().CreateDelegate>(); } /// Creates an getter delegate for a property @@ -75,7 +75,7 @@ public static GetterHandler CreateGetterHandler(PropertyInfo propert getGenerator.Emit(OpCodes.Call, getMethodInfo); getGenerator.Emit(OpCodes.Ret); - return (GetterHandler)dynamicGet.Generate().CreateDelegate(typeof(GetterHandler)); + return dynamicGet.Generate().CreateDelegate>(); } /// Creates an getter delegate for a field @@ -94,7 +94,7 @@ public static GetterHandler CreateGetterHandler(FieldInfo fieldInfo) getGenerator.Emit(OpCodes.Ldfld, fieldInfo); getGenerator.Emit(OpCodes.Ret); - return (GetterHandler)dynamicGet.Generate().CreateDelegate(typeof(GetterHandler)); + return dynamicGet.Generate().CreateDelegate>(); } /// Creates an getter delegate for a field (with a list of possible field names) @@ -139,7 +139,7 @@ public static SetterHandler CreateSetterHandler(PropertyInfo propert setGenerator.Emit(OpCodes.Call, setMethodInfo); setGenerator.Emit(OpCodes.Ret); - return (SetterHandler)dynamicSet.Generate().CreateDelegate(typeof(SetterHandler)); + return dynamicSet.Generate().CreateDelegate>(); } /// Creates an setter delegate for a field @@ -159,7 +159,7 @@ public static SetterHandler CreateSetterHandler(FieldInfo fieldInfo) setGenerator.Emit(OpCodes.Stfld, fieldInfo); setGenerator.Emit(OpCodes.Ret); - return (SetterHandler)dynamicSet.Generate().CreateDelegate(typeof(SetterHandler)); + return dynamicSet.Generate().CreateDelegate>(); } static DynamicMethodDefinition CreateGetDynamicMethod(Type type) => new($"DynamicGet_{type.Name}", typeof(S), [typeof(T)]); diff --git a/Harmony/Extras/MethodInvoker.cs b/Harmony/Extras/MethodInvoker.cs index e1fbc2c4..64df2a47 100644 --- a/Harmony/Extras/MethodInvoker.cs +++ b/Harmony/Extras/MethodInvoker.cs @@ -140,7 +140,7 @@ public static FastInvokeHandler GetHandler(MethodInfo methodInfo, bool directBox Emit(il, OpCodes.Ret); - var invoder = (FastInvokeHandler)dynamicMethod.Generate().CreateDelegate(typeof(FastInvokeHandler)); + var invoder = dynamicMethod.Generate().CreateDelegate(); return invoder; } diff --git a/Harmony/GlobalSuppressions.cs b/Harmony/GlobalSuppressions.cs index bc560d54..9675c408 100644 --- a/Harmony/GlobalSuppressions.cs +++ b/Harmony/GlobalSuppressions.cs @@ -13,6 +13,7 @@ [assembly: SuppressMessage("CodeQuality", "IDE0079")] [assembly: SuppressMessage("", "IDE0251")] +[assembly: SuppressMessage("", "IDE0130")] [assembly: SuppressMessage("Style", "IDE0270")] [assembly: SuppressMessage("Usage", "CA2211")] [assembly: SuppressMessage("GeneratedRegex", "SYSLIB1045")] diff --git a/Harmony/Tools/AccessTools.cs b/Harmony/Tools/AccessTools.cs index 3a5593a6..32b1871a 100644 --- a/Harmony/Tools/AccessTools.cs +++ b/Harmony/Tools/AccessTools.cs @@ -1532,15 +1532,16 @@ public static FieldRef StaticFieldRefAccess(FieldInfo fieldInfo) /// Use this if the generic arguments of doesn't represent the delegate's /// arguments, and calling this function fails /// A delegate of given to given + /// /// - /// + /// /// Delegate invocation is more performant and more convenient to use than /// at a one-time setup cost. - /// - /// + /// + /// /// Works for both type of static and instance methods, both open and closed (a.k.a. unbound and bound) instance methods, /// and both class and struct methods. - /// + /// /// /// public static DelegateType MethodDelegate(MethodInfo method, object instance = null, bool virtualCall = true, Type[] delegateArgs = null) where DelegateType : Delegate @@ -1734,7 +1735,7 @@ public static DelegateType MethodDelegate(string typeColonName, ob /// A delegate of given to the method specified via [] /// attributes on /// - /// This calls with the method and virtualCall arguments + /// This calls with the method and virtualCall arguments /// determined from the [] attributes on , /// and the given (for closed instance delegates). /// diff --git a/Harmony/Tools/FileLog.cs b/Harmony/Tools/FileLog.cs index b2d1e7d9..6c8beacc 100644 --- a/Harmony/Tools/FileLog.cs +++ b/Harmony/Tools/FileLog.cs @@ -4,6 +4,9 @@ using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; +#if NET9_0_OR_GREATER +using System.Threading; +#endif namespace HarmonyLib { @@ -11,7 +14,11 @@ namespace HarmonyLib /// public static class FileLog { +#if NET9_0_OR_GREATER + private static readonly Lock fileLock = new(); +#else private static readonly object fileLock = new(); +#endif private static bool _logPathInited; private static string _logPath; diff --git a/Harmony/Tools/Tools.cs b/Harmony/Tools/Tools.cs index c20a83cf..bb9fbf44 100644 --- a/Harmony/Tools/Tools.cs +++ b/Harmony/Tools/Tools.cs @@ -80,7 +80,7 @@ internal static FieldRef FieldRefAccess(FieldInfo fieldInfo, bool ne } il.Emit(OpCodes.Ret); - return (FieldRef)dm.Generate().CreateDelegate(typeof(FieldRef)); + return dm.Generate().CreateDelegate>(); } internal static StructFieldRef StructFieldRefAccess(FieldInfo fieldInfo) where T : struct @@ -95,7 +95,7 @@ internal static StructFieldRef StructFieldRefAccess(FieldInfo fieldI il.Emit(OpCodes.Ldflda, fieldInfo); il.Emit(OpCodes.Ret); - return (StructFieldRef)dm.Generate().CreateDelegate(typeof(StructFieldRef)); + return dm.Generate().CreateDelegate>(); } internal static FieldRef StaticFieldRefAccess(FieldInfo fieldInfo) @@ -111,7 +111,7 @@ internal static FieldRef StaticFieldRefAccess(FieldInfo fieldInfo) il.Emit(OpCodes.Ldsflda, fieldInfo); il.Emit(OpCodes.Ret); - return (FieldRef)dm.Generate().CreateDelegate(typeof(FieldRef)); + return dm.Generate().CreateDelegate>(); } internal static FieldInfo GetInstanceField(Type type, string fieldName) diff --git a/HarmonyTests/GlobalSuppressions.cs b/HarmonyTests/GlobalSuppressions.cs index 0c8f7490..d30067c3 100644 --- a/HarmonyTests/GlobalSuppressions.cs +++ b/HarmonyTests/GlobalSuppressions.cs @@ -21,6 +21,8 @@ [assembly: SuppressMessage("Style", "IDE0066")] [assembly: SuppressMessage("Style", "IDE0251")] +[assembly: SuppressMessage("", "IDE0130")] + [assembly: SuppressMessage("Usage", "CA2208")] [assembly: SuppressMessage("Usage", "CA2211")] diff --git a/HarmonyTests/Patching/Assets/ArgumentCases.cs b/HarmonyTests/Patching/Assets/ArgumentCases.cs index e7996e88..37494709 100644 --- a/HarmonyTests/Patching/Assets/ArgumentCases.cs +++ b/HarmonyTests/Patching/Assets/ArgumentCases.cs @@ -2,6 +2,7 @@ using NUnit.Framework; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace HarmonyLibTests.Assets @@ -149,7 +150,11 @@ public class NullableResults private string s = "foo"; [MethodImpl(MethodImplOptions.NoInlining)] - public bool? Method() => false; + public bool? Method() + { + _ = s; + return false; + } } [HarmonyPatch(typeof(NullableResults), nameof(NullableResults.Method))] diff --git a/HarmonyTests/Patching/FinalizerPatches.cs b/HarmonyTests/Patching/FinalizerPatches.cs index 637aff87..eca9e6c6 100644 --- a/HarmonyTests/Patching/FinalizerPatches.cs +++ b/HarmonyTests/Patching/FinalizerPatches.cs @@ -335,7 +335,7 @@ private void AssertThrownException() { Assert.NotNull(info, "info should not be null"); Assert.NotNull(info["outerexception"], "Should throw an exception"); - Assert.IsInstanceOf(typeof(E), info["outerexception"]); + Assert.IsInstanceOf(info["outerexception"]); } private void AssertNullExceptionInput() @@ -350,7 +350,7 @@ private void AssertExceptionInput() Assert.NotNull(info, "info should not be null"); Assert.True(info.ContainsKey("exception"), "Finalizer should have an exception field"); Assert.NotNull(info["exception"], "Finalizer should get an exception input"); - Assert.IsInstanceOf(typeof(E), info["exception"]); + Assert.IsInstanceOf(info["exception"]); } } } diff --git a/HarmonyTests/Patching/Specials.cs b/HarmonyTests/Patching/Specials.cs index 640cb356..da2e2161 100644 --- a/HarmonyTests/Patching/Specials.cs +++ b/HarmonyTests/Patching/Specials.cs @@ -351,7 +351,7 @@ public void Test_PatchExceptionWithCleanup2() catch (HarmonyException ex) { Assert.NotNull(ex.InnerException); - Assert.IsInstanceOf(typeof(ArgumentException), ex.InnerException); + Assert.IsInstanceOf(ex.InnerException); Assert.AreEqual("Test", ex.InnerException.Message); return; } diff --git a/HarmonyTests/Tools/TestAccessTools.cs b/HarmonyTests/Tools/TestAccessTools.cs index e8a95c46..f365bc8a 100644 --- a/HarmonyTests/Tools/TestAccessTools.cs +++ b/HarmonyTests/Tools/TestAccessTools.cs @@ -410,7 +410,7 @@ public void Test_AccessTools_MethodDelegate_ClosedInstanceDelegates() Assert.AreEqual("base test 456 793 2", AccessTools.MethodDelegate(baseTest, derivedInstance, virtualCall: false)(456, ref f)); // derivedTest => baseTest automatically for virtual calls Assert.AreEqual("base test 456 794 3", AccessTools.MethodDelegate(derivedTest, baseInstance, virtualCall: true)(456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate(derivedTest, baseInstance, virtualCall: false)(456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate(derivedTest, baseInstance, virtualCall: false)(456, ref f)); Assert.AreEqual("derived test 456 795 3", AccessTools.MethodDelegate(derivedTest, derivedInstance, virtualCall: true)(456, ref f)); Assert.AreEqual("derived test 456 796 4", AccessTools.MethodDelegate(derivedTest, derivedInstance, virtualCall: false)(456, ref f)); Assert.AreEqual("struct result 456 797 1", AccessTools.MethodDelegate(structTest, structInstance, virtualCall: true)(456, ref f)); @@ -425,11 +425,11 @@ public void Test_AccessTools_MethodDelegate_ClosedInstanceDelegates_InterfaceMet var derivedInstance = new Derived(); var structInstance = new Struct(); Assert.AreEqual("base test 456 790 1", AccessTools.MethodDelegate(interfaceTest, baseInstance, virtualCall: true)(456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate(interfaceTest, baseInstance, virtualCall: false)(456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate(interfaceTest, baseInstance, virtualCall: false)(456, ref f)); Assert.AreEqual("derived test 456 791 1", AccessTools.MethodDelegate(interfaceTest, derivedInstance, virtualCall: true)(456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate(interfaceTest, derivedInstance, virtualCall: false)(456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate(interfaceTest, derivedInstance, virtualCall: false)(456, ref f)); Assert.AreEqual("struct result 456 792 1", AccessTools.MethodDelegate(interfaceTest, structInstance, virtualCall: true)(456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate(interfaceTest, structInstance, virtualCall: false)(456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate(interfaceTest, structInstance, virtualCall: false)(456, ref f)); } [Test] @@ -445,9 +445,9 @@ public void Test_AccessTools_MethodDelegate_OpenInstanceDelegates() Assert.AreEqual("base test 456 793 2", AccessTools.MethodDelegate>(baseTest, virtualCall: false)(derivedInstance, 456, ref f)); // derivedTest => baseTest automatically for virtual calls Assert.AreEqual("base test 456 794 3", AccessTools.MethodDelegate>(derivedTest, virtualCall: true)(baseInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(baseInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(baseInstance, 456, ref f)); Assert.AreEqual("derived test 456 795 3", AccessTools.MethodDelegate>(derivedTest, virtualCall: true)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(derivedInstance, 456, ref f)); // AccessTools.MethodDelegate>(derivedTest)(baseInstance, 456, ref f); // expected compile error // AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(baseInstance, 456, ref f); // expected compile error Assert.AreEqual("derived test 456 796 4", AccessTools.MethodDelegate>(derivedTest, virtualCall: true)(derivedInstance, 456, ref f)); @@ -464,15 +464,15 @@ public void Test_AccessTools_MethodDelegate_OpenInstanceDelegates_DelegateInterf var derivedInstance = new Derived(); var structInstance = new Struct(); Assert.AreEqual("base test 456 790 1", AccessTools.MethodDelegate>(baseTest, virtualCall: true)(baseInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(baseTest, virtualCall: false)(baseInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(baseTest, virtualCall: false)(baseInstance, 456, ref f)); Assert.AreEqual("derived test 456 791 1", AccessTools.MethodDelegate>(baseTest, virtualCall: true)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(baseTest, virtualCall: false)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(baseTest, virtualCall: false)(derivedInstance, 456, ref f)); Assert.AreEqual("base test 456 792 2", AccessTools.MethodDelegate>(derivedTest, virtualCall: true)(baseInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(baseInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(baseInstance, 456, ref f)); Assert.AreEqual("derived test 456 793 2", AccessTools.MethodDelegate>(derivedTest, virtualCall: true)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(derivedTest, virtualCall: false)(derivedInstance, 456, ref f)); Assert.AreEqual("struct result 456 794 1", AccessTools.MethodDelegate>(structTest, virtualCall: true)(structInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(structTest, virtualCall: false)(structInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(structTest, virtualCall: false)(structInstance, 456, ref f)); } [Test] @@ -483,21 +483,21 @@ public void Test_AccessTools_MethodDelegate_OpenInstanceDelegates_InterfaceMetho var derivedInstance = new Derived(); var structInstance = new Struct(); Assert.AreEqual("base test 456 790 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(baseInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(baseInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(baseInstance, 456, ref f)); Assert.AreEqual("derived test 456 791 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(derivedInstance, 456, ref f)); Assert.AreEqual("struct result 456 792 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(structInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(structInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(structInstance, 456, ref f)); Assert.AreEqual("base test 456 793 2", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(baseInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(baseInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(baseInstance, 456, ref f)); Assert.AreEqual("derived test 456 794 2", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(derivedInstance, 456, ref f)); // AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(baseInstance, 456, ref f)); // expected compile error // AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(baseInstance, 456, ref f)); // expected compile error Assert.AreEqual("derived test 456 795 3", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(derivedInstance, 456, ref f)); Assert.AreEqual("struct result 456 796 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(structInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(structInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(structInstance, 456, ref f)); } [Test] @@ -525,21 +525,21 @@ public void Test_AccessTools_MethodDelegate_OpenInstanceDelegates_BoxedArgs() var delegateArgs_Derived = new Type[] { typeof(Derived), typeof(object), typeof(float).MakeByRefType() }; var delegateArgs_Struct = new Type[] { typeof(Struct), typeof(object), typeof(float).MakeByRefType() }; // Assert.AreEqual("base test 456 790 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true, delegateArgs: delegateArgs_IInterface)(baseInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_IInterface)(baseInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_IInterface)(baseInstance, 456, ref f)); // Assert.AreEqual("derived test 456 791 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true, delegateArgs: delegateArgs_IInterface)(derivedInstance, 456, ref f)); - // _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_IInterface)(derivedInstance, 456, ref f)); + // _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_IInterface)(derivedInstance, 456, ref f)); // Assert.AreEqual("struct result 456 792 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true, delegateArgs: delegateArgs_IInterface)(structInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_IInterface)(structInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_IInterface)(structInstance, 456, ref f)); // Assert.AreEqual("base test 456 793 2", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true, delegateArgs: delegateArgs_Base)(baseInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Base)(baseInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Base)(baseInstance, 456, ref f)); // Assert.AreEqual("derived test 456 794 2", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true, delegateArgs: delegateArgs_Base)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Base)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Base)(derivedInstance, 456, ref f)); // AccessTools.MethodDelegate>(interfaceTest, virtualCall: true)(baseInstance, 456, ref f)); // expected compile error // AccessTools.MethodDelegate>(interfaceTest, virtualCall: false)(baseInstance, 456, ref f)); // expected compile error // Assert.AreEqual("derived test 456 795 3", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true, delegateArgs: delegateArgs_Derived)(derivedInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Derived)(derivedInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Derived)(derivedInstance, 456, ref f)); // Assert.AreEqual("struct result 456 796 1", AccessTools.MethodDelegate>(interfaceTest, virtualCall: true, delegateArgs: delegateArgs_Struct)(structInstance, 456, ref f)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Struct)(structInstance, 456, ref f)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(interfaceTest, virtualCall: false, delegateArgs: delegateArgs_Struct)(structInstance, 456, ref f)); } [Test] @@ -554,10 +554,10 @@ public void Test_AccessTools_MethodDelegate_StaticDelegates_InterfaceMethod() [Test] public void Test_AccessTools_MethodDelegate_InvalidDelegates() { - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate(interfaceTest)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(baseTest)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(derivedTest)); - _ = Assert.Throws(typeof(ArgumentException), () => AccessTools.MethodDelegate>(structTest)); + _ = Assert.Throws(() => AccessTools.MethodDelegate(interfaceTest)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(baseTest)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(derivedTest)); + _ = Assert.Throws(() => AccessTools.MethodDelegate>(structTest)); } delegate string MethodDel(int n, ref float f); diff --git a/HarmonyTests/Tools/TestFieldRefAccess.cs b/HarmonyTests/Tools/TestFieldRefAccess.cs index 65e04a3d..8a864943 100644 --- a/HarmonyTests/Tools/TestFieldRefAccess.cs +++ b/HarmonyTests/Tools/TestFieldRefAccess.cs @@ -717,7 +717,7 @@ public void Test_ClassInstance_PrivateStructFieldType() [Test] public void Test_ClassInstance_ListOfPrivateStructFieldType() { - Assert.Multiple(() => + Assert.Multiple(static () => { var field = AccessTools.Field(typeof(AccessToolsClass), "field8"); var expectedCaseToConstraint = expectedCaseToConstraint_ClassInstance; @@ -745,7 +745,7 @@ static IList TestValue() field, TestValue(), expectedCaseToConstraint_ClassInstance_StructT); // List is invariant - List cannot be cast to List nor vice versa, // so can't do TestSuite_Class(...). - Assert.That(TestValue(), Is.Not.InstanceOf(typeof(List))); + Assert.That(TestValue(), Is.Not.InstanceOf>()); TestSuite_Class( field, TestValue(), expectedCaseToConstraint); TestSuite_Class(