且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中声明泛型类型

更新时间:2022-12-23 10:43:34

是的,你可以做到这一点,而不诉诸分裂,解析或使用的 codeDOM 和的 CSHARP codeProvider

Yes, you can do it without resorting to splitting, parsing or manipulating strings by using CodeDom and CSharpCodeProvider:

using CodeDom;
using Microsoft.CSharp;

// ...

Type yourType = typeof(List<string>);  // for example

using (var provider = new CSharpCodeProvider())
{
    var typeRef = new CodeTypeReference(yourType);
    Console.WriteLine(provider.GetTypeOutput(typeRef));
}

(您可能需要做一些额外的字符串操作,以删除命名空间preFIX例如,如果希望输出为名单,LT;字符串&GT; 而比 System.Collections.Generic.List&LT;字符串&GT;