8

如何声明和使用泛型接口(请参阅命名空间 Sample2)以与命名空间 Sample1 中的类相同的方式工作?

我知道有一种解决方法(请参阅命名空间 Sample2Modified),但这不是我想要实现的。


与类一起工作

使用类时,这很好用:

namespace Sample1
{
    public class ClassContainer<T1, T2>
        where T1 : T2
    { }

    public class ClassParent
    { }

    public class ClassChild : ClassParent
    { }

    public class ClassSampleDoesWork<TItem>
        where TItem : ClassParent
    {
        ClassContainer<IEnumerable<TItem>, IEnumerable<ClassParent>> SomeProperty { get; set; }
    }
}

不适用于接口

但是,对于接口,编译器在 ClassSampleDoesNotWork 中声明属性时会发出此警告:

Error 16 The type 'System.Collections.Generic.IEnumerable<TItem>'
cannot be used as type parameter 'T1' in the generic type or method
'Sample2.IInterfaceContainer<T1,T2>'. There is no implicit reference
conversion from 'System.Collections.Generic.IEnumerable<TItem>' to
'System.Collections.Generic.IEnumerable<Sample2.IInterfaceParent>'.

代码:

namespace Sample2
{
    public interface IInterfaceContainer<T1, T2>
        where T1 : T2
    { }

    public interface IInterfaceParent
    { }

    public interface IInterfaceChild : IInterfaceParent
    { }

    public class ClassSampleDoesNotWork<TItem>
        where TItem : IInterfaceParent
    {
        IInterfaceContainer<IEnumerable<TItem>, IEnumerable<IInterfaceParent>> SomeProperty { get; set; }
    }
}

修改版有效,但这不是我想要的

如果我将类修改为使用 TEnumerableOfItem 而不是 TItem,它会起作用:

namespace Sample2Modified
{
    public interface IInterfaceContainer<T1, T2>
        where T1 : T2
    { }

    public interface IInterfaceParent
    { }

    public interface IInterfaceChild : IInterfaceParent
    { }

    public class ClassSampleModifiedDoesWork<TEnumerableOfItem>
        where TEnumerableOfItem : IEnumerable<IInterfaceParent>
    {
        IInterfaceContainer<TEnumerableOfItem, IEnumerable<IInterfaceParent>> SomeProperty { get; set; }
    }
}
4

1 回答 1

5

Try to add class constraint to the TItem:

namespace Sample2
{
    public interface IInterfaceContainer<T1, T2>            
        where T1 : T2
    { }

    public interface IInterfaceParent
    { }

    public interface IInterfaceChild : IInterfaceParent
    { }

    public class ClassSampleDoesNotWork<TItem>
        where TItem : class, IInterfaceParent
    {
        IInterfaceContainer<IEnumerable<TItem>, IEnumerable<IInterfaceParent>> SomeProperty { get; set; }
    }
}

This works because variance only works for reference-types (or there is an identity conversion). It isn't known that TItem is reference type, unless you add : class. Read this article for more information. Here is a sample code to demonstrate this behavior:

IEnumerable<Object> items = new List<int>(); // Compiler error.
于 2013-07-08T14:25:31.637 回答