C#泛型获取类型名称 [英] C# Get Generic Type Name

查看:1250
本文介绍了C#泛型获取类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些方法来得到一个类型,当 type.IsGenericType = 真正

 键入T = typeof运算(名单<串GT;); 
MessageBox.Show(..);



我想要的,是一个消息框,列表显示......我该怎么办呢?


解决方案

 类型T = ...; 

如果(t.IsGenericType)
{
型G = t.GetGenericTypeDefinition();

MessageBox.Show(g.Name); //显示List`1

MessageBox.Show(g.Name.Remove(g.Name.IndexOf('''))); //显示列表
}


I need some way to get the Name of a Type, when type.IsGenericType = true.

    Type t = typeof(List<String>);
    MessageBox.Show( ..?.. );

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆