检查对象是否是泛型集合
我们正在动态构建一些 SQL 语句,并且正在利用 IN 运算符。如果我们的值是值的集合,例如:
List<Guid> guids = new List<Guid>()
我希望能够向我的子句构建器提供“guid”,让它验证类型,如果它是可枚举的,则创建一个子句,如下所示:
IN ( {Guid1}, {Guid2}, {Guid3} )
检查该值是否为 IEnumerable,如下所示:
if (value is IEnumerable)
当传入字符串时会掉落(这种情况经常发生:))。验证此类情况的最佳方法是什么?
We are dynamically building some SQL statements and we are utilizing the IN operator. If our value is a collection of values such that:
List<Guid> guids = new List<Guid>()
I want to be able to provider 'guids' to my clause builder, have it verify the type and if it is enumerable create a clause like:
IN ( {Guid1}, {Guid2}, {Guid3} )
Checking that the value is IEnumerable like this:
if (value is IEnumerable)
falls down when a string is passed in (which happens pretty regularly :) ). What is the best way to validate this type of condition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
How about:
您可以尝试将
value.GetType().IsGenericType
与您对 IEnumerable 的检查结合起来。You could try
value.GetType().IsGenericType
in combination with your check for IEnumerable.怎么样:
如果您期望 Guid 实例,那就更好了,不是吗?
What about :
It's better if you expect Guid instances, isn't it ?