diff --git a/Harmony/Public/CodeInstruction.cs b/Harmony/Public/CodeInstruction.cs
index e826b156..e9d7dc50 100644
--- a/Harmony/Public/CodeInstruction.cs
+++ b/Harmony/Public/CodeInstruction.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
@@ -11,7 +11,7 @@ namespace HarmonyLib
public class CodeInstruction
{
/// The opcode
- ///
+ ///
public OpCode opcode;
/// The operand
@@ -19,13 +19,18 @@ public class CodeInstruction
public object operand;
/// All labels defined on this instruction
- ///
+ ///
public List labels = new List();
/// All exception block boundaries defined on this instruction
- ///
+ ///
public List blocks = new List();
+ // Internal parameterless constructor that AccessTools.CreateInstance can use, ensuring that labels/blocks are initialized.
+ internal CodeInstruction()
+ {
+ }
+
/// Creates a new CodeInstruction with a given opcode and optional operand
/// The opcode
/// The operand
diff --git a/Harmony/Tools/AccessTools.cs b/Harmony/Tools/AccessTools.cs
index de72c246..b4990436 100644
--- a/Harmony/Tools/AccessTools.cs
+++ b/Harmony/Tools/AccessTools.cs
@@ -1692,9 +1692,10 @@ public static object CreateInstance(Type type)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
- var ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, new Type[0], null);
+ var ctor = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, binder: null,
+ CallingConventions.Any, new Type[0], modifiers: null);
if (ctor is object)
- return Activator.CreateInstance(type);
+ return ctor.Invoke(null);
return FormatterServices.GetUninitializedObject(type);
}
diff --git a/HarmonyTests/Tools/TestAccessTools.cs b/HarmonyTests/Tools/TestAccessTools.cs
index 4412785c..2975e6cb 100644
--- a/HarmonyTests/Tools/TestAccessTools.cs
+++ b/HarmonyTests/Tools/TestAccessTools.cs
@@ -365,10 +365,10 @@ public void Test_AccessTools_CreateInstance()
Assert.IsTrue(AccessTools.CreateInstance().constructorCalled);
Assert.IsFalse(AccessTools.CreateInstance().constructorCalled);
Assert.IsTrue(AccessTools.CreateInstance().constructorCalled);
- Assert.IsFalse(AccessTools.CreateInstance().constructorCalled);
+ Assert.IsTrue(AccessTools.CreateInstance().constructorCalled);
var instruction = AccessTools.CreateInstance();
- Assert.Null(instruction.labels);
- Assert.Null(instruction.blocks);
+ Assert.NotNull(instruction.labels);
+ Assert.NotNull(instruction.blocks);
}
[Test]