[英]Generic Interface with Type T and where
I try my first steps into generic interfaces and have this interface: 我尝试进入通用接口的第一步,并使用以下接口:
public interface ISetting<T>
{
T Value { get; }
}
And a implementation like this: 和这样的实现:
public class MySetting : ISetting<DateTime>
{
DateTime Value { get; }
}
Now my target is, that I can write my code like this in my service layer: 现在我的目标是,可以在服务层中编写如下代码:
public class MyService
{
public DateTime GetMySetting()
{
return MyDataLayer.GetSettings<MySetting>();
}
}
So in my DataLayer i am struggling with my implementation of my generic and where: 因此,在我的DataLayer中,我正在努力实现通用的实现,以及在哪里:
public T ReadOrAddOne<T>() where T : ISetting<????>
{
// My Code goes here
}
So how to tell my ISetting which Type I am awaiting? 那么如何告诉我的ISetting我正在等待哪种类型?
You just need to define another letter to represent that type. 您只需要定义另一个字母即可表示该类型。
public T ReadOrAddOne<T,R>() where T : ISetting<R>
{
return default(T);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.