2012-07-13 94 views
0

我嘗試寫看起來像這樣爲什麼這個通用接口定義是錯誤的?

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的IEnumerable的定義一個接口。

我在做什麼錯了? 理念是實現類將成爲一個通用的屬性包集

+0

用'object'替換所有泛型和你設置。 – ja72 2012-07-13 06:16:30

回答

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>

UPDATE:

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 
} 
+0

那我該如何使用這個集合作爲PropertyBag?如果我將通用類型添加到集合接口? – SudheerKovalam 2012-07-13 06:12:26

+0

那麼,你不能像你想要的那樣使用泛型。 – 2012-07-13 06:15:25

+0

那麼我有沒有一種方法可以定義一個類似物業包的界面? – SudheerKovalam 2012-07-13 06:16:23

相關問題