Lib.Harmony does not work in LINQPad 9 due to missing App Switch
Hello, I am using newest beta of LINQPad 9 (9.2.1) and I am trying to launch code that uses Lib.Harmony
, but I'm experiencing BinaryFormatter serialization and deserialization have been removed. See https://aka.ms/binaryformatter for more information.
exception.
This is my code:
public static class Program { static void Main() { var harmony = new Harmony("com.example.patch"); harmony.PatchAll(); Test(true, true).Dump(); } private static bool Test(bool a, bool b) { if (a && b) return false; if (a) if (b) return true; return false; } } [HarmonyPatch(typeof(Program), "Test")] class MyPatch { private static bool Prefix(bool a, bool b, ref bool __result) { __result = true; // Replace return value return false; // Skip original method } }
this is probably because of missing app switch System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization
, this library checks if this switch is present and if not it assumes BinaryFormatter is available link to their code. In LINQPad this switch is not set, while programs launched from Visual Studio targetting .NET 8 have it set to false. Would it be possible to set this switch to false as well?
Comments
-
as a workaround it is possible to set the switch manually:
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", false);
-
LINQPad does not set this switch:
AppContext.TryGetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", out bool isEnabled).Dump(); isEnabled.Dump(); // false
Isn't the result that you're getting expected? Under .NET 8 it works; under .NET 9 it fails because the binary engine has been removed from .NET 9.