Harmony
Harmony copied to clipboard
Harmony AccessTools.MakeDeepCopy fails when target contains a constant or static readonly field
trafficstars
Describe the bug
When using AccessTools.MakeDeepCopy on a class which contains a constant field, Harmony will cause a System.FieldAccessException: Cannot set a constant field.
To Reproduce
- Create an executable program
- Create another class with a constant field in it
- In the Main method, create an instance of the class and call MakeDeepCopy on it
System.FieldAccessException: Cannot set a constant field.
at System.Reflection.MdFieldInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
at HarmonyLib.Traverse.SetValue(Object value)
at HarmonyLib.AccessTools.<>c__DisplayClass102_0.<MakeDeepCopy>b__0(String name, Traverse src, Traverse dst)
at HarmonyLib.Traverse.<>c__DisplayClass39_0.<IterateFields>b__0(String f)
at System.Collections.Generic.List`1.ForEach(Action`1 action)
at HarmonyLib.Traverse.IterateFields(Object source, Object target, Action`3 action)
at HarmonyLib.AccessTools.MakeDeepCopy(Object source, Type resultType, Func`4 processor, String pathRoot)
at HarmonyLib.AccessTools.MakeDeepCopy[T](Object source)
at Harmonyissue.Program.Main(String[] args) in D:\Programming\Projects\Modding\HarmonyStuff\Harmonyissue\Harmonyissue\Program.cs:line 16
Expected behavior Const fields should be ignored when creating a DeepCopy
Screenshots / Code
Runtime environment (please complete the following information):
OS:Windows 10, 64bit.NET Version:I've managed to reproduce this error in .NET Framework 4.8.1 (Unity and non-Unity runtime), .NET 6.0 and .NET 8.0. I assume it's an issue on all versionsHarmony Version:Tested on Harmony 2.3.3 and Harmony 2.3.1.1.Runtime:I've reproduced it in bothPathfinder: Wrath of the RighteousandWarhammer 40,000: Rogue Traderadditionally I can reproduce this in a simple console application