C#中声明泛型类型 [英] C# declaration of generic type
本文介绍了C#中声明泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
时有可能得到与反射得到一个类型的C#姓名,如:
Is it possible to get the "c# name" of a type obtained with reflexion like:
System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
我想获得:
List<String>
是否有可能不分割字符串?例如,使用反射
Is it possible without splitting strings? For example, using Reflection.
谢谢!
推荐答案
是的,你可以做到这一点,而不诉诸分裂,解析或使用的 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;
)
这篇关于C#中声明泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文