gpt4 book ai didi

C# 泛型类型的声明

转载 作者:行者123 更新时间:2023-11-30 13:40:52 25 4
gpt4 key购买 nike

是否有可能获得通过反射获得的类型的“c# 名称”,例如:

System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

我想得到:

List<String>

是否可以不拆分字符串?例如,使用反射。

谢谢!

最佳答案

不是直接的,但您可以检查类型本身来弄清楚。

public static string TypeName(Type t) {
if (!t.IsGenericType) return t.Name;

StringBuilder ret = new StringBuilder();
ret.Append(t.Name).Append("<");

bool first = true;
foreach(var arg in t.GetGenericArguments()) {
if (!first) ret.Append(", ");
first = false;

ret.Append(TypeName(arg));
}

ret.Append(">");
return ret.ToString();
}

关于C# 泛型类型的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6636173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com