为什么泛型类型的名称在.NET堆栈跟踪错位? [英] Why are the names of generic types mangled in a .NET stack trace?
问题描述
我有一个例外,从C#的方法,采用一个通用的清单作为paremeter被抛出。
私有静态无效DoWork的(列表<类名>一种)
{
}
在它抛出异常,堆栈跟踪显示一个'1而不是对列表中的类名。为什么是这样?这就是堆栈跟踪了。在DoWork的(List`1一)
...
。
...
之所以会堆栈跟踪由CLR,而不是C#生成的。因此,它使用CLR类型名称与C#类型名称。
在元数据提供给泛型类型的类型名称(在C#和VB.Net)的格式TypeName`Number其中,
- 类型名:在泛型参数的abscence类型的名称
- 编号:该类型泛型参数计数
这也是为什么它是合法的,有几个泛型类,相同的名称,但泛型参数的不同数字。在CLR水平,他们有不同的类型名称。
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).
...
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.
这篇关于为什么泛型类型的名称在.NET堆栈跟踪错位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!