为什么这个通用的接口定义错了?

Sud*_*lam 0 c# generics interface generic-programming

我正在尝试编写一个看起来像这样的界面

public interface IPropertyGroupCollection
{
    IEnumerable<IPropertyGroup> _Propertygroups { get;}
}

public interface IPropertyGroup
{
    IEnumerable<IProperty<T, U, V>> _conditions { get; }
}

public interface IProperty<T, U, V>
{
    T _p1 { get; }
    U _p2 { get; }
    V _p3 { get; }
}

public class Property<T, U, V> : IProperty<T, U, V>
{
    //Some Implementation
}
Run Code Online (Sandbox Code Playgroud)

我继续为_Conditions的可枚举定义获取编译错误.

我究竟做错了什么?Idea是实现类将提供通用属性包集合

Jak*_*cki 7

这是因为你没有声明T,U和V:

public interface IPropertyGroup<T, U, V>
{
    IEnumerable<IProperty<T, U, V>> _conditions { get; }
}
Run Code Online (Sandbox Code Playgroud)

您还必须添加泛型类型IPropertyGroupCollection.

请记住,尽管它们来自相同的通用"模板" IProperty<bool,bool,bool>,IProperty<int,int,int>但它是一种不同的类型.您无法创建集合IProperty<T, U, V>,您只能创建IProperty<bool, bool, bool>或集合IProperty<int int, int>.

更新:

public interface IPropertyGroupCollection
{
    IEnumerable<IPropertyGroup> _Propertygroups { get;}
}

public interface IPropertyGroup
{
    IEnumerable<IProperty> _conditions { get; }
}

public interface IProperty
{
}

public interface IProperty<T, U, V> : IProperty
{
    T _p1 { get; }
    U _p2 { get; }
    V _p3 { get; }
}

public class Property<T, U, V> : IProperty<T, U, V>
{
    //Some Implementation
}
Run Code Online (Sandbox Code Playgroud)