yasirkula / UnityIngameDebugConsole

A uGUI based console to see debug messages and execute commands during gameplay in Unity
MIT License
2.05k stars 217 forks source link

Suppress ReflectionTypeLoadException #64

Closed laicasaane closed 2 years ago

laicasaane commented 2 years ago

My project includes some assemblies (Microsoft.CodeAnalysis, Microsoft.CodeAnalysis.CSharp) to support code generation. Inside Unity Editor, they are just broken and not supposed to work. They are there just to make sure my Editor code compiled without error. So they are not supposed to be searched by the DebugLogConsole class, otherwise ReflectionTypeLoadException will be thrown. And this is the case atm.

image

Ignoring user-defined assemblies seems to be impossible with the way DebugLogConsole is implemented. I suggest we can just ignore this exception.

laicasaane commented 2 years ago

Unity 2021.2.5

Couldn't search assembly for [ConsoleMethod] attributes: Microsoft.CodeAnalysis
System.Reflection.ReflectionTypeLoadException: Exception of type 'System.Reflection.ReflectionTypeLoadException' was thrown.
Could not load type of field 'Microsoft.CodeAnalysis.CommandLineArguments:<Encoding>k__BackingField' (38) due to: Could not resolve type with token 01000062 from typeref (expected class 'System.Text.Encoding' in assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') assembly:System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Text.Encoding member:(null)
  at (wrapper managed-to-native) System.Reflection.Assembly.GetTypes(System.Reflection.Assembly,bool)
  at System.Reflection.RuntimeAssembly.GetExportedTypes () [0x00000] in <00c558282d074245ab3496e2d108079b>:0 
  at IngameDebugConsole.DebugLogConsole..cctor () [0x00651] in D:\Projects\ZG\ZBase\ZBase.Libraries\Library\PackageCache\com.yasirkula.ingamedebugconsole@1.5.2\Plugins\IngameDebugConsole\Scripts\DebugLogConsole.cs:187 
System.TypeLoadException: Could not load type of field 'Microsoft.CodeAnalysis.CommandLineArguments:<Encoding>k__BackingField' (38) due to: Could not resolve type with token 01000062 from typeref (expected class 'System.Text.Encoding' in assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') assembly:System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Text.Encoding member:(null)
UnityEngine.Debug:LogError (object)
IngameDebugConsole.DebugLogConsole:.cctor () (at Library/PackageCache/com.yasirkula.ingamedebugconsole@1.5.2/Plugins/IngameDebugConsole/Scripts/DebugLogConsole.cs:204)
IngameDebugConsole.DebugLogManager:OnEnable () (at Library/PackageCache/com.yasirkula.ingamedebugconsole@1.5.2/Plugins/IngameDebugConsole/Scripts/DebugLogManager.cs:515)
yasirkula commented 2 years ago

Thank you for bringing this to my attention. I was unaware of this exception's existence. Will resolve this issue in the next update which may take some time because I'm working on a couple of other plugins at the moment.

P.S. In the meantime, you can simply catch this exception at the problematic line(s) which is what I'll do myself.

yasirkula commented 2 years ago

On second thought, I've decided to prioritize this issue 🤷‍♂️