How can I tell if my generic type supports a certain interface and convert back and forthto that interface for a function?

leora

I have a method that looks like this:

private IEnumerable<T> QueryCollection<T>() where T : BaseObj
{
      IEnumerable<T> items = query<T>();
      return items;
}

I now have a situation where I want to filter this items collection IF the "T" supports a certain interface (it might not so i can't simply add it as a constraint of T). So i want something like this:

private IEnumerable<T> QueryCollection<T>() where T : BaseObj
{
     IEnumerable<T> items = query<T>();

     if (typeOf(T).GetInterface(ITeamFilterable) != null)
     {
           items = FilterByTeams(items);
     }
     return items;
}

What is the recommended way of checking if my generic Type supports a certain interface **and then if YES, then

  1. Use that within the method to filter a collection
  2. But still return the collection at type "T" in the overall method

NOTE: FilterByTeams takes in an:

   IEnumerable<ITeamFilterable> 

AND returns

   IEnumerable<ITeamFilterable>

Do I need to cast the collection 2 times (one to convert to List of the interface and then again to convert back to list of T?)

Lucas Trzesniewski

I'm afraid you can't use is/as in this case unless you can start enumerating the query.

You can use reflection like that:

private IEnumerable<T> QueryCollection<T>() where T : BaseObj
{
     IEnumerable<T> items = query<T>();

     if (typeof(ITeamFilterable).IsAssignableFrom(typeof(T)))
           items = (IEnumerable<T>)(object)FilterByTeams(items.Cast<ITeamFilterable>());

     return items;
}

But if you can enumerate the query, you can do that to avoid reflection (assuming you have no null items):

private IEnumerable<T> QueryCollection<T>() where T : BaseObj
{
     IEnumerable<T> items = query<T>();
     ICollection<T> itemsCollection = items as ICollection<T> ?? items.ToList();

     if (itemsCollection.Count > 0)
     {
         var firstItem = itemsCollection.First();
         if (firstItem is ITeamFilterable)
             return (IEnumerable<T>)(object)FilterByTeams(itemsCollection.Cast<ITeamFilterable>());
     }

     return itemsCollection;
}

The casts will work since ITeamFilterable is a T and IEnumerable<T> is covariant, but you have to cast to object in-between to satisfy the generics constraints. Using these casts, there's no need to copy items around.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I tell if my interface{} is a pointer?

From Dev

Why can I not cast to a generic interface from a type with a generic argument which is a subtype of the generic argument in my interface?

From Dev

How can I tell which network interface my computer is using?

From Dev

Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

From Dev

convert interface{} to certain type

From Dev

How can I restrict an Interface to be implemented by only a generic type

From Dev

How can you bound the type of a generic interface to another generic interface?

From Dev

How can I tell if my Intel Core CPU supports SLAT?

From Dev

Why can't I convert concrete type to interface while using generic constraints

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Dev

How can I type assert a reflect.Value struct back to an interface that I know it implements?

From Dev

How can I extend a non generic interface into a generic one?

From Dev

Can I ignore a generic type in a C# interface?

From Dev

From an Interface how do I return a generic/dynamic type from a function

From Dev

From an Interface how do I return a generic/dynamic type from a function

From Dev

How can I resolve the Class type for generic parameter, when the parameter value is being passed as an interface?

From Dev

How can I obtain the type parameter of a generic interface from an implementing class?

From Dev

How to use generic function interface method type parameters in a lambda

From Dev

Generic interface to convert user type to array

From Dev

How can a generic interface refer to a pointer to its type parameter?

From Dev

How can I define a Typescript interface for a function?

From Dev

How can I determine if Java generic implements a particular interface?

From Dev

How can I implement a generic interface in Rhino JS?

From Dev

Error in enum returning function in interface with generic type

From Dev

Error in enum returning function in interface with generic type

From Dev

Generic function for type that implements common interface

From Dev

Interface with generic object of the interface type

From Dev

How can I tell if the wireless network card in my laptop supports 802.11n?

From Dev

How can I tell which SATA Revision my Laptop Motherboard Supports?

Related Related

  1. 1

    How can I tell if my interface{} is a pointer?

  2. 2

    Why can I not cast to a generic interface from a type with a generic argument which is a subtype of the generic argument in my interface?

  3. 3

    How can I tell which network interface my computer is using?

  4. 4

    Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

  5. 5

    convert interface{} to certain type

  6. 6

    How can I restrict an Interface to be implemented by only a generic type

  7. 7

    How can you bound the type of a generic interface to another generic interface?

  8. 8

    How can I tell if my Intel Core CPU supports SLAT?

  9. 9

    Why can't I convert concrete type to interface while using generic constraints

  10. 10

    How can I define a return type of void for a function in a Typescript interface?

  11. 11

    How can I type assert a reflect.Value struct back to an interface that I know it implements?

  12. 12

    How can I extend a non generic interface into a generic one?

  13. 13

    Can I ignore a generic type in a C# interface?

  14. 14

    From an Interface how do I return a generic/dynamic type from a function

  15. 15

    From an Interface how do I return a generic/dynamic type from a function

  16. 16

    How can I resolve the Class type for generic parameter, when the parameter value is being passed as an interface?

  17. 17

    How can I obtain the type parameter of a generic interface from an implementing class?

  18. 18

    How to use generic function interface method type parameters in a lambda

  19. 19

    Generic interface to convert user type to array

  20. 20

    How can a generic interface refer to a pointer to its type parameter?

  21. 21

    How can I define a Typescript interface for a function?

  22. 22

    How can I determine if Java generic implements a particular interface?

  23. 23

    How can I implement a generic interface in Rhino JS?

  24. 24

    Error in enum returning function in interface with generic type

  25. 25

    Error in enum returning function in interface with generic type

  26. 26

    Generic function for type that implements common interface

  27. 27

    Interface with generic object of the interface type

  28. 28

    How can I tell if the wireless network card in my laptop supports 802.11n?

  29. 29

    How can I tell which SATA Revision my Laptop Motherboard Supports?

HotTag

Archive