这是我编写的将逗号列表转换为 T 数组的片段:
public static T[] ToArray<T>(this string s, params char[] seps)
{
if (typeof(T) == typeof(int))
{
return s.Split(seps.Length > 0 ? seps : new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(id => int.Parse(id))
.Cast<T>()
.ToArray();
}
else throw new Exception("cannot convert to " + typeof(T).Name);
}
我需要为我想要支持的每种类型设置一个案例。
有没有更好的方法来编码这种东西?