Using generics in interfaces

How to allow mine to CookieDatabe shared in the following code? I get an ad compile-time error ICookieService2.

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

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

My mistake:

Cannot find type name or namespace 'T' (are you missing the using directive or assembly references?)

I want to ICookieService2contain general data in it. Thanks!

Change . Would it block me into one Tto build any ICookieService2?

Edit 2 I am trying to do the following:

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);
+5
source share
8 answers

It looks like you have 3 options.

Make ICookieService2 Shared

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

ICookieService2:

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

, :

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);
}
+2

, :

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

ICookieService2 , T .

ICookieService2<string> ICookieService2<int> ..

EDIT: , , , . - .

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 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);
+1

#, Java ICookieService2 <T> <T> - CookieData.

0

, , , .NET .

CookieData < > CookieData <int> , , . ICookieService2 ( ICookieService2 <T> ) , ICookieService2 , .

ICookieData CookieData <T> ICookieData ( Stefan).

, # 4.0 ( ). , , , , , .

0

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

, T . ( # (, int == System.Integer ..).

0

All Articles