C# 获取泛型类型名称 [英] C# Get Generic Type Name
本文介绍了C# 获取泛型类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当 type.IsGenericType
= true
时,我需要某种方法来获取类型的名称.
I need some way to get the Name of a Type, when type.IsGenericType
= true
.
Type t = typeof(List<String>);
MessageBox.Show( ..?.. );
我想要的是弹出一个带有 List
显示的消息框...我该怎么做?
What I want, is a message box to pop up with List
showing... how can I do that?
推荐答案
Type t = ...;
if (t.IsGenericType)
{
Type g = t.GetGenericTypeDefinition();
MessageBox.Show(g.Name); // displays "List`1"
MessageBox.Show(g.Name.Remove(g.Name.IndexOf('`'))); // displays "List"
}
这篇关于C# 获取泛型类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文