ILSpy fails to decode custom attribute blob for non-generic types with ` (backtick) in their name
Steps to reproduce
- Open a .dll with some attributes with a type name containing ` (backtick), but not being generic
- Observe that ILSpy will fail to decode the custom attribute blobs, even though it's completely valid as per ECMA-335 (and works at runtime)
Actual behavior
Expected behavior
[!NOTE] I took this screenshot by replacing ` with ' in the type names, so ILSpy would work correctly.
Details
- ILSpy version 9.1.0.7988+03b7444943e720b3134d296c0c8dd3876f8ea4ce
- .NET version 8.0.18+ef853a71052646a42abf17e888ec6d9a69614ad9
Can you please provide an example binary for us to look at?
Here's a simple assembly that just has a failing attribute as a custom attribute on the assembly, along with the types to support it: example.zip
Yes, there is no rule that says «Do not use the backtick symbol in names» But according to the rules described in the specification you should not use reserved names. The naming guidelines also advise against using character sets, such as the backtick symbol.
In this case no compiler will allow you to compile the name with backtick symbol because it is reserved for types with a generic parameter. The backtick symbol enables the type parser to identify the presence of a generic parameter based on the name. It is not possible to parse names with the backtick symbol in 100% of cases. You could add a workaround to catch such cases, which might help, but I think it would be considered interference outside the compiler, e.g., an obfuscator. Therefore, there is no point in adding such a workaround.
One possible solution to this problem is to display an error message containing the raw name. However, this would require transporting the name with the error state from the bottom of the parser.
In this case no compiler will allow you to compile the name with backtick symbol
This is actually not true, F# allows this with an exception type, though I'm fairly certain it is a bug: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AbEAzAzmgFxAEsMAfCABxgDsACAZQE9cCYBbAWACgf2IAJgFcMMOgFEEAQ3aVRdALw86KugSbU6AQQAUROgBUNMAJSLlqy8RoALGFGIFtBAg+BC2OkxZUwEYGEoCYgh6AAMwgxgw1gieHzoAbQAeXXVqCGxkiKiYggiAPhMCgF0E0ScpRToABh4gA=
Edit: the F# issue https://github.com/dotnet/fsharp/issues/18763
The best and easiest way to fix this issue would be to move away from ILSpy maintaining its own parser for this and instead using the new System.Reflection.Metadata.TypeName API that was introduced in .NET 9:
https://github.com/dotnet/runtime/issues/97566
https://learn.microsoft.com/en-us/dotnet/api/system.reflection.metadata.typename?view=net-9.0
PRs welcome!
The best and easiest way to fix this issue would be to move away from ILSpy maintaining its own parser
Best, yes - easiest, hell no. It's way easier to fix the existing parser to just interpret a backtick that does not form a valid generic name as normal name character.
We also might not be able to go latest SRM in ics.d because of Roslyn.
@Sergio0694 We likely fixed this by adopting SRM TypeName internally, can you please try your assembly again? Thanks!