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

时间:2012-07-13 06:02:10

标签: 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
}

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

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

1 个答案:

答案 0 :(得分:7)

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

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

您还必须将通用类型添加到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
}
相关问题