Avoid System.Private.CoreLib Reference from .NET Core 3.1 Process Runner #846
Replies: 1 comment 3 replies
-
When you use Use an |
Beta Was this translation helpful? Give feedback.
-
When you use Use an |
Beta Was this translation helpful? Give feedback.
-
What is the best way to avoid having Mono.Cecil add a reference to the System.Private.CoreLib assembly when the external process executing my IL modification code (outside my control) was upgraded to target .NET Core 3.1? Alternatively, is there a way within my IL modification code to properly remove the System.Private.CoreLib reference and fix-up any usage to point to mscorlib instead?
Background:
I have a custom ILPostProcessor assembly that functions properly in Unity 2020.3 with a Unity project that is using .NET Framework API compatibility. The ILPostProcessor is executed by the Unity ILPostProcessorRunner.exe against all assemblies in the project and each assembly is built against .NET Framework 4.0. However, when upgrading the same project to Unity 2021.3 Mono.Cecil adds a reference to System.Private.CoreLib to all modified assemblies.
In debugging the issue I have found the root cause to be that ILPostProcessRunner.exe was changed from targeting .NET Core 2.0 to .NET Core 3.1 between Unity 2020.3 and 2021.3. With the ILPostProcessorRunner change, imports of anything that references types like System.Void (i.e. a method return type) now point to System.Private.CoreLib rather than mscorlib as they did with the Unity 2020.3 ILPostProcessorRunner.exe; this is of course the case even though the assemblies being processed are still built against .NET Framework 4.0. This poses an issue in Mono.Cecil.DefaultReflectionImporter.ImportMethod (Import.cs at line 397) when, for example, importing the constructor of an Attribute class since this will force Mono.Cecil to add the System.Private.CoreLib reference due to the System.Void return type and it all seems to be outside my control.
As an example, the Attribute class constructor import line is as follows:
assemblyMainModule.ImportReference(typeof(CustomILProcessedAttribute).GetConstructors().First())
Debugging my IL Weaver code run via Unity 2020.3 verses Unity 2021.3 clearly shows the issue even external to any Mono.Cecil calls as the assembly qualified name for System.Void for a 2020.3 run is in mscorelib where it is in System.Private.CoreLib for a 2021.3 run.
Also, I did test code to remove the System.Private.CoreLib reference after all modifications are complete but that caused a TypeLoadException for System.Action`3 that I could not figure out; mscorlib of course remains referenced in all cases.
Beta Was this translation helpful? Give feedback.
All reactions