我正在尝试在IEnumerable上编写一个扩展方法,它只适用于值类型和字符串.
public static string MyMethod<T>(this IEnumerable<T> source) where T : struct, string
Run Code Online (Sandbox Code Playgroud)
但是'string'不是有效的约束,因为它是一个密封的类.
有没有办法做到这一点?
编辑:
我实际上要做的是为动态构造的SQL中的"IN"子句准备一个值列表.
我有很多代码实例,例如以下我要清理的代码:
sb.AppendLine(string.Format("AND value IN ({0})", string.Join(",", Values.Select(x => x.ToSQL()).ToArray())));
Run Code Online (Sandbox Code Playgroud)
ToSQL()具有处理SqlInjection的代码.