I have a method that accepts a parameter obj of type System.Object
Now I want to check if the actual type of obj is:
The first way I thought of is:
if (obj is IEnumerable)
// obj is a collection
But System.String implements IEnumerable, and I don't want to treat string as a collection.
The second way I thought is testing for ICollection instead of IEnumerable, since IEnumerable is more of a potential collection than an actual one. This would leave out string, but also ICollection-Of-T because it does not inherit ICollection (IEnumerable-Of-T is the only generic collection abstraction that's backwards compatible - it inherits IEnumerable).
So I guess the best way is:
if (obj is string)
// not a collection
else if (obj is IEnumerable)
// collection
else
// not a collection
Is there a better way?
I think you're over complicating this a bit. If you really want to use IEnumerable but exclude System.String, why not just do that directly in code?
public static bool IsCollection(object obj) {
return obj is IEnumerable && !(obj is String);
}
If you really only want to test:
bool isCollection = obj.GetType().GetInterfaces()
.Any(iface => iface.GetGenericTypeDefinition() == typeof(ICollection<>))
But frankly, if you really only want to special-case string
(why, by the way?), then just do so. If you test for ICollection<>
, you will treat the result of a LINQ query as "non-collection", for example, for no good reason.
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