The type '' cannot be used as type parameter 'T' in the generic type or method

后端 未结 1 1583
星月不相逢
星月不相逢 2021-01-28 22:02

I have created this generic method

public IEnumerable GetByID(IEnumerable source, int id) where T : IFields
{
   return source.Where(x         


        
相关标签:
1条回答
  • 2021-01-28 22:03

    The obvious reason for this error message is that Items doesn't implement IFields.

    In other words, your Items type need to have this:

    public class Items : IFields
                         ^-----^
    

    If you don't have this, then Items is not a valid type for your GetByID<T> method since it doesn't implement this interface.

    This is true even if the Items type just happens to have the right members. Unless you've explicitly stated that those members also implement the interface, by doing what I showed above, then that is not enough. So even if your Items type have the id property, you still have to state explicitly that the type implements the interface.

    0 讨论(0)
提交回复
热议问题