コンマリストを 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);
}
サポートしたいタイプごとにケースを配置する必要があります。
この種のものをコーディングするより良い方法はありますか?