【发布时间】:2011-02-24 13:30:24
【问题描述】:
我有一个 IEnumerable 的 ToString 扩展方法,它将其转换为字符串列表,如下所示:
public static string ToString<T>(this IEnumerable<T> theSource,
string theSeparator) where T : class
{
string[] array =
theSource.Where(n => n != null).Select(n => n.ToString()).ToArray();
return string.Join(theSeparator, array);
}
我现在想对枚举数组做类似的事情:给定 theXStatuses,一个 XStatus 枚举值数组,我想得到一个包含由 theSeparator 分隔的枚举值的字符串。由于某种原因,上述扩展方法不适用于 XStatus[]。所以我尝试了
public static string ToString1<T>(this IEnumerable<T> theSource,string theSeparator)
where T : Enum
但后来我得到一个错误“不能使用...'System.Enum'...作为类型参数约束。
有什么方法可以实现吗?
【问题讨论】:
-
这是 C#,对吧?您应该将该标签添加到您的问题中(我会这样做,但我不能 100% 确定它是 C#)。
标签: c# enums extension-methods