C# 在接口中使用泛型
如何允许我的C# 在接口中使用泛型,c#,generics,collections,interface,C#,Generics,Collections,Interface,如何允许我的CookieData在以下代码中是泛型的?我在声明ICookieService2时遇到编译时错误 public struct CookieData<T> { T Value { get; set; } DateTime Expires { get; set; } } public interface ICookieService2: IDictionary<string, CookieData<T>> { // ... }
CookieData
在以下代码中是泛型的?我在声明ICookieService2
时遇到编译时错误
public struct CookieData<T>
{
T Value { get; set; }
DateTime Expires { get; set; }
}
public interface ICookieService2: IDictionary<string, CookieData<T>>
{
// ...
}
您还必须将ICookieService2设置为通用:
public interface ICookieService2<T>: IDictionary<string, CookieData<T>>
{
// ...
}
公共接口ICookieService2:IDictionary
{
// ...
}
好的,下面是您想要的:
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);
}
公共接口ICookie数据库
{
日期时间过期{get;set;}
}
公共结构CookieData:ICookieDataBase
{
公共T值{get;set;}
公共日期时间过期{get;set;}
}
公共类ICookieService:IDictionary
{
// ...
}
公共工作
{
var intCookie=
新CookieData{Value=27,Expires=DateTime.Now};
var stringCookie=
新CookieData{Value=“Bob”,Expires=DateTime.Now};
ICookieService cs=GetICookieService();
cs.Add(intCookie);
cs.Add(stringCookie);
}
不确定C,但Java的方法是同时声明您的ICookieService2
具有
参数,或者将
指定为CookieData
中的具体内容。我想您想要的是:
public interface ICookieService2<T>: IDictionary<string, CookieData<T>>
{
// ...
}
公共接口ICookieService2:IDictionary
{
// ...
}
目前ICookieService2不是泛型的,因此没有定义T
这允许您创建实现ICookieService2
或ICookieService2
等的类
编辑:
对于你最近的要求,我认为这完全取决于你需要什么。然而,像这样的事情可能对你有用
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;
}
}
}
公共接口ICookieData
{
对象值{get;}//如果您需要能够从CookieService进行设置,生活会变得更加艰难,但仍然可行。
日期时间过期{get;}
}
公共结构CookieDate:ICookieData
{
T值{get;set;}
日期时间过期{get;set;}
对象ICookieData.Value
{
得到
{
返回值;
}
}
}
然后CookieService可以是或有一个列表,您可以添加CookieData和CookieData。如果您需要从CookieService编写(设置),它会稍微复杂一些,最好不要使用泛型。但是,如果您只需要能够检索CookieData,那么这可能适合您。您需要一个非通用接口来执行此操作:
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;
}
}
}
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);
公共接口ICookieData
{
//您需要这个来获取值,而不知道它的类型
对象非类型值{get;}
//你还需要什么。。。
类型数据类型{get;}
日期时间过期{get;set;}
}
公共结构CookieData:ICookieData
{
//ICookieData实现
公共对象非类型值{get{return Value;}}
公共类型数据类型{get{return typeof(T);}
公共日期时间过期{get;set;}
//通用实现
T值{get;set;}
}
公共接口ICookieService2:IDictionary
{
// ...
}
CookieData intCookie={Value=27,Expires=DateTime.Now};
CookieData stringCookie={Value=“Bob”,Expires=DateTime.Now};
CookieService2 cs=新CookieService2();
cs.Add(intCookie);
cs.Add(stringCookie);
这里似乎有3个选项
使iCokieService2通用
public interface ICookieService2<T> : IDictionary<string, CookieData<T> {
...
}
公共接口ICookieService2:IDictionary如果我正确理解您的问题,您希望将泛型类型视为相同,但在当前的.NET实现中,您无法做到这一点
CookieData不能作为CookieData传递它们实际上是不同的类型,泛型类型不相关,不能像它们一样强制转换或传递。将ICookieService2创建为一个开放泛型(意味着ICookieService2)并不能解决问题,因为这样一来,每个不同的类型都会有一个不同的ICookieService2类型,并且所有这些类型都会在不同的字典中
您可以创建一个ICookieData接口,并在CookieData中实现它,并使用ICookieData将项目存储在字典中(正如Stefan已经建议的那样)
你可以在C#4.0中做你想做的事(有一些限制)。在那之前,有一篇关于它的文章,你必须将它作为一个非泛型类型传递,这对他们来说是常见的,比如一个接口,或者让value参数成为字典上的一个对象,因为如果你使用原语作为类型参数,你将不得不对它进行大小写才能使用它。你可以尝试一下
public struct CookieData<T>
where T : Object
{
T Value { get; set; }
DateTime Expires { get; set; }
}
公共结构CookieData
其中T:Object
{
T值{get;set;}
日期时间过期{get;set;}
}
这样,T的所有实例都将被强制具有基本类型的对象。(C#中几乎所有内容(例如int==System.Integer等)
public interface ICookieService : IDictionary<string, CookieData<int>> {}
public struct CookieData<T>
where T : Object
{
T Value { get; set; }
DateTime Expires { get; set; }
}