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?
To examine a generic type and its type parametersGet an instance of Type that represents the generic type. In the following code, the type is obtained using the C# typeof operator ( GetType in Visual Basic, typeid in Visual C++). See the Type class topic for other ways to get a Type object.
Generic means the general form, not specific. In C#, generic means not specific to a particular data type. C# allows you to define generic classes, interfaces, abstract classes, fields, methods, static methods, properties, events, delegates, and operators using the type parameter and without the specific data type.
The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.
Better performance. Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types. Generic delegates enable type-safe callbacks without the need to create multiple delegate classes.
How about:
if(value .GetType().IsGenericType && value is IEnumerable)
You could try value.GetType().IsGenericType
in combination with your check for IEnumerable.
What about :
value is IEnumerable<Guid>
It's better if you expect Guid instances, isn't it ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With