Generic constraint ignores co-variance

后端 未结 1 1838
栀梦
栀梦 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<T>(IEnumerable<T> p) where T : class, ISomeInterface
    {
        IEnumerable<ISomeInterface> e = p;
        // or
        TestMethod(p);
    }
    

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

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