【发布时间】:2018-10-31 02:30:09
【问题描述】:
有没有一种简单的方法可以在c#中获取包含模板参数的类的名称?
I.E
Print( new List<int>{ 100, 1001});
产生以下输出之一:
List<int>
List<Int32>
System.Collections.Generics.List<System.Int32>
首选前 2 个中的一个,但 Type.FullName 不是一个可行的选择。太冗长了。
不想要的过于冗长的结果。
System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=235hosehoue5h]]
请注意,如果存在快捷方式,我只需要它。我不是要求完全实现此类功能。
【问题讨论】:
-
这些不是“模板参数”,这些是“通用类型参数”。它们不一样。 C# 中的泛型与 C++ 中的模板不同。
-
Console.WriteLine(typeof(List<int>))产生System.Collections.Generic.List'1[System.Int32]。这有什么问题? -
缺少尖括号,不利于嵌套模板类型的可读性 =>
System.Console.WriteLine(typeof(List<Tuple<List<int>,List<char>>>));打印System.Collections.Generic.List'1[System.Tuple'2[System.Collections.Generic.List'1[System.Int32],System.Collections.Generic.List'1[System.Char]]] -
How about this?。也许不是我所说的“简单”......
-
@UberFace 没有任何遗漏,这只是反射符号。也许值得问一下你想用这些字符串做什么?例如,出于调试原因,这无关紧要。
标签: c# reflection