Search code examples
c#.netdecompiling

Can't repoduce call stack in one of my programs


I'm encountering an issue while working with my application. I suspect that the version running in production might not be fully aligned with the version in my repository, but I'm not entirely sure.

Every day, I receive an email summary that includes the following call stack:

2024-09-17 13:15:06.697 +02:00 [Warning] Error parsing filename parts for file "C:\temp.txt".
System.ArgumentException: Requested value 'CHCR' was not found.
   at System.Enum.TryParseByName(RuntimeType enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, UInt64& result)
   at System.Enum.TryParse(Type enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, Object& result)
   at System.Enum.TryParse(Type enumType, String value, Boolean ignoreCase, Boolean throwOnFailure, Object& result)
   at Application.GetFileNameParts(String file) in C:\Application\FileResolver.cs:line 229

However, when I review the GetFileNameParts method in my code (via IL Spy), I don’t see any explicit call to System.Enum.TryParse. The code only calls System.Enum.Parse, which is what I expect. Below is the relevant code snippet:

try {
    FilePart fileParts = new();
    string messageType = "CHCR";
    fileParts.MessageType = (FileMessageType)Enum.Parse(typeof(FileMessageType), messageType, true);
} catch (Exception e) {
    _log.LogWarning(e, "Error parsing filename parts for file {file}.", file);
    return null;
}

I’m struggling to understand how System.Enum.TryParse is appearing in the stack trace when I’m using Enum.Parse. Could this be some version mismatch, or is there something else at play that I might be overlooking?

All details which I have provided are in section above.


Solution

  • Enum.Parse calls Enum.TryParse.

    See sourcecode of .NET.

    The body of Enum.Parse is probably inlined by the compiler and does not appear on the callstack.