How do I allow my CookieData
to be generic in the following code? I get an compile-time error on the declaration of ICookieService2
.
public struct CookieData<T>
{
T Value { get; set; }
DateTime Expires { get; set; }
}
public interface ICookieService2: IDictionary<string, CookieData<T>>
{
// ...
}
My error is:
The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
I am wanting ICookieService2
to have generic data inserted into it. Thanks!
Edit Won't that lock me into a single T
for the construction of any ICookieService2
?
Edit 2 What I am trying to do is 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);
It looks like you have 3 options here
Make ICookieService2 generic
public interface ICookieService2<T> : IDictionary<string, CookieData<T> {
...
}
Create a non-generic base class for CookieData and use that in the interface
public interface ICookieData {}
public class CookieData<T>: ICookieData{}
public interface ICookieService2 : IDictionary<string, ICookieData> {}
Pick a concrete implementation
public interface ICookieService : IDictionary<string, CookieData<int>> {}
You must make ICookieService2 generic as well:
public interface ICookieService2<T>: IDictionary<string, CookieData<T>>
{
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With