2009-06-01 60 views
5

如何讓我的CookieData在下面的代碼中是泛型的?我在ICookieService2的聲明中遇到編譯時錯誤。在接口中使用泛型

public struct CookieData<T> 
{ 
    T Value { get; set; } 
    DateTime Expires { get; set; } 
} 

public interface ICookieService2: IDictionary<string, CookieData<T>> 
{ 
    // ... 
} 

我的錯誤是:

類型或命名空間名稱 'T' 找不到(是否缺少using指令或程序集引用?)

我想要ICookieService2將通用數據插入到其中。謝謝!

編輯這難道不會鎖定我成爲一個T爲建設任何ICookieService2

編輯2我所試圖做的是以下幾點:

CookieData<int> intCookie = { Value = 27, Expires = DateTime.Now }; 
CookieData<string> stringCookie = { Value = "Bob", Expires = DateTime.Now }; 

CookieService2 cs = new CookieService2(); 
cs.Add(intCookie); 
cs.Add(stringCookie); 

回答

7

看起來你有3個選擇這裏

讓ICookieService2通用

public interface ICookieService2<T> : IDictionary<string, CookieData<T> { 
... 
} 

創建一個非泛型基地用於CookieData並在界面中使用

public interface ICookieData {} 
public class CookieData<T>: ICookieData{} 
public interface ICookieService2 : IDictionary<string, ICookieData> {} 

選擇一個具體的實施

public interface ICookieService : IDictionary<string, CookieData<int>> {} 
4

您必須ICookieService2通用以及:

public interface ICookieService2<T>: IDictionary<string, CookieData<T>> 
{ 
    // ... 
} 
2

OK,這裏有你想要的東西:

public interface ICookieDataBase 
{ 
    DateTime Expires { get; set; } 
} 

public struct CookieData<T> : ICookieDataBase 
{ 
    public T Value { get; set; } 
    public DateTime Expires { get; set; } 
} 

public class ICookieService : IDictionary<string, ICookieDataBase> 
{ 
    // ... 
} 

public void DoWork() 
{ 
    var intCookie = 
     new CookieData<int> { Value = 27, Expires = DateTime.Now }; 

    var stringCookie = 
     new CookieData<string> { Value = "Bob", Expires = DateTime.Now }; 

    ICookieService cs = GetICookieService(); 
    cs.Add(intCookie); 
    cs.Add(stringCookie); 
} 
0

不確定C#,但Java的方式也是聲明喲ur ICookieService2擁有<T>參數,或者指定<T>作爲CookieData中的具體內容。

+0

好的,從上面看到的答案這是正確的:) – Jorn 2009-06-01 23:09:21

1

我想你想要的是:

public interface ICookieService2<T>: IDictionary<string, CookieData<T>> 
{ 
    // ... 
} 

目前ICookieService2是不是通用的,所以沒有定義噸。

這允許你創建的類實現ICookieService2<string>ICookieService2<int>

編輯: 響應最新的要求,我認爲這真的取決於它正是你需要的。但是,像這樣的東西可能適合你。

public interface ICookieData 
{ 
    object Value {get;} // if you need to be able to set from CookieService, life gets harder, but still doable. 
    DateTime Expires {get;} 
} 

public struct CookieDate<T> : ICookieData 
{ 
    T Value {get;set;} 
    DateTime Expires {get;set;} 

    object ICookieData.Value 
    { 
     get 
     { 
      return Value; 
     } 
    } 
} 

然後CookieService可以或有一個列表,你將能夠同時添加CookieData和CookieData。如果你需要從CookieService寫入(設置),它稍微複雜一點,可能更好的是不使用泛型。但是,如果你只需要能夠檢索CookieData,那麼這可能適用於你。

1

你需要一個非通用接口做到這一點:

public interface ICookieData 
{ 
    // you need this to get the value without knowing it's type 
    object UntypedValue { get; } 
    // whatever you need additionally ... 
    Type DataType { get; } 

    DateTime Expires { get; set; } 
} 

public struct CookieData<T> : ICookieData 
{ 
    // ICookieData implementation 
    public object UntypedValue { get { return Value; } } 
    public Type DataType { get { return typeof(T); } } 
    public DateTime Expires { get; set; } 

    // generic implementation 
    T Value { get; set; } 
} 

public interface ICookieService2: IDictionary<string, ICookieData> 
{ 
    // ... 
} 

CookieData<int> intCookie = { Value = 27, Expires = DateTime.Now }; 
CookieData<string> stringCookie = { Value = "Bob", Expires = DateTime.Now }; 

CookieService2 cs = new CookieService2(); 
cs.Add(intCookie); 
cs.Add(stringCookie); 
0

如果我理解你的問題正確,您是想治療泛型類型就像他們是相同的,但在目前的.NET實現你不能做到這一點。

CookieData <串>不能作爲CookieData <INT>傳遞他們實際上是不同的類型,泛型類型是不相關的,不能投或通過像他們一樣。將ICookieService2創建爲開放式泛型(意思是ICookieService2 <T>)並不能解決問題,因爲那樣您將針對每種不同的類型使用不同的ICookieService2類型,並且所有這些類型都將位於不同的字典中。

您可以創建一個ICookieData界面和CookieData <牛逼>實現,並使用ICookieData存儲在字典中的項目(如斯特凡已經暗示)。

你可以在C#4.0中做什麼(有一些限制)。有一篇關於它的文章here直到那時,你必須將它作爲它們通用的非泛型類型傳遞,比如接口,或者只是將value參數作爲字典上的對象,因爲你將不得不大小寫無論如何,如果你使用基元作爲類型參數,它就可以使用它。

0

你可以嘗試

public struct CookieData<T> 
    where T : Object 
{ 
    T Value { get; set; } 
    DateTime Expires { get; set; } 
} 

這樣T的實例將被迫有對象的基本類型。 (幾乎在C#中的一切(例如int == System.Integer等)。