简体   繁体   English

T型通用接口,其中

[英]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.

相关问题 泛型函数,泛型类型是任何接口 - generic function where generic type is any interface 检查对象是否使用通用接口实现接口 <T> 泛型类型是T的子代 - Checking if an object implements an interface with a generic <T> where the generic type is a child of T 按类型调用通用方法,其中类型为接口 - Invoke generic method by type where type is interface 为什么我不能将一个项目转换为泛型类型——其中泛型类型是一个接口,并且在检查该项目是否实现了所述接口之后? - Why can't I cast an item to a generic type — where the generic type is an interface, and after checking that the item implements said interface? T实现接口的通用方法<T> - Generic method where T implements Interface<T> C# IEnumerable 中的协变/逆变<t>其中 T 是通用类型,接口与 class</t> - C# Covariance / Contravariance in IEnumerable<T> where T is generic type, interface vs class 通用方法,其中 T 是实现接口的列表 - Generic Method where T is List that implements interface 参数化通用接口 - 创建字典 <Type, Interface<T> &gt;? - Parameterize a generic interface - Create a Dictionary<Type, Interface<T>>? 泛型类型,其中 T 派生自泛型类型 - Generic type where T is derived from a generic type 定义IList时遇到问题 <T> 其中T是通用接口 - Trouble defining an IList<T> where T is an generic interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM