0

I've tried the following:

if ((type == typeof(ICollection<T>)))

if (type.GetInterfaces().Contains(System.Collections.Generic.ICollection<T>))

Neither of these seem to catch anything at all, even things that I know are ICollections. If I try to look for IEnumerable, then it catches too many things, including strings.

There was another answer on SO that said to use this:

if(Obj is ICollection)
{
    //Derived from ICollection
}

That doesn't even compile for me. I have to specify that it's generic. When I look at the object type in the debugger, this is what I see:

{Name = "ICollection`1" FullName = "System.Collections.Generic.ICollection`1[[object1, object1Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}

I don't know how to look for that type. What I want is any type that is specifically an ICollection of any type.

Edit1: Here's the definition of the type I'm trying to identify:

public virtual ICollection<object1> objects { get; set; }
mrplainswalker
  • 701
  • 4
  • 8
  • 26
  • can you please provide your code to have a look? its not clear what you want to achieve – MCoder Apr 04 '18 at 15:20
  • Why do you need to determine if something is a `ICollection`? – juharr Apr 04 '18 at 15:23
  • I'm not sure what other code is relevant. I can give you the definition of what I'm trying to identify: – mrplainswalker Apr 04 '18 at 15:27
  • I'm guessing you've got an `object` that you're dealing with. Where does it come from? If it's passed into a method have you considered using overloading and/or generics to handle different types. Or maybe just explain in broader terms what this code is part of. – juharr Apr 04 '18 at 15:29
  • 1
    As far as the type system is concerned, `ICollection` isn't a thing. `ICollection` would be a thing. Or `ICollection`. You could use either of those with the `is` operator. But the need to do this at runtime at all indicates a design flaw much earlier in your process. This kind of thing should be identified by the **compiler**, where you have, say, a generic method that expects or returns an `ICollection` and _only_ an `ICollection`. – Joel Coehoorn Apr 04 '18 at 15:31
  • So there's no way to determine if something is a collection of ANY type? I have to check specific types? – mrplainswalker Apr 04 '18 at 15:32
  • @mrplainswalker The duplicate has answers that will do what you want. However they require using reflection which you should try to avoid if possible. If you explain the problem you're trying to solve with this code then we may be able to give you an alternative solution. – juharr Apr 04 '18 at 15:34
  • See my amended comment. – Joel Coehoorn Apr 04 '18 at 15:35
  • I'm sort of trapped by both pre-existing design and the way Entity Framework auto-generates code. I need to loop through all the properties of a table-based model so I can map them to a grid, but I want to exclude things that aren't actually in the table, like for example collections that it creates when you have a foreign key. – mrplainswalker Apr 04 '18 at 15:39

0 Answers0