Why are the names of generic types mangled in a .NET stack trace?

Deadly 提交于 2019-12-30 18:26:13

问题


I have an exception being thrown from a C# method, that takes a generic list as a paremeter.

private static void DoWork(List<ClassName> a)
{
}

When it throws an exception, the stack trace shows an `1 instead of the class name for the list. Why is this? This is what the stack trace has.

... 
at DoWork(List`1 a).
...

回答1:


The reason why is that the stack trace is generated by the CLR and not C#. Hence it uses CLR type names vs. C# type names.

The type names given to generic types in metadata (in both C# and VB.Net) have the form TypeName`Number where

  • TypeName: Name of the type in the abscence of generic parameters
  • Number: Count of generic parameters on the type

This is also why it's legal to have several generic classes which the same name but differing numbers of generic parameters. At a CLR level they have different type names.



来源:https://stackoverflow.com/questions/1369294/why-are-the-names-of-generic-types-mangled-in-a-net-stack-trace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!