Generic constraint ignores co-variance

后端 未结 1 1840
栀梦
栀梦 2020-12-11 04:31

Let\'s say we have an interface like

public interface IEnumerable
{ /*...*/ }

that is co-variant in T.

1条回答
  •  执笔经年
    2020-12-11 05:06

    Change your GenericMethod and add generic constraint class:

    public void GenericMethod(IEnumerable p) where T : class, ISomeInterface
    {
        IEnumerable e = p;
        // or
        TestMethod(p);
    }
    

    Covariance does not support structs, so we need to tell that we want to use classes only.

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