Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通用接口c#_C#_Generics - Fatal编程技术网

通用接口c#

通用接口c#,c#,generics,C#,Generics,Hi想知道将接口作为通用参数的通用接口的用例。它可以用来做什么,可以解决什么问题。我不能认为用例将接口作为通用参数而不是类对象 比如说 public interface IProvideA<T> { T GetAVal(string val); } 公共接口IProvideA { T GetAVal(字符串val); } 现在创建了一个将接口作为泛型参数的泛型类型 public interface IProvideAlpha<T> where T:IProvi

Hi想知道将接口作为通用参数的通用接口的用例。它可以用来做什么,可以解决什么问题。我不能认为用例将接口作为通用参数而不是类对象

比如说

public interface IProvideA<T>
{
   T GetAVal(string val);
}
公共接口IProvideA
{
T GetAVal(字符串val);
}
现在创建了一个将接口作为泛型参数的泛型类型

public interface  IProvideAlpha<T> where T:IProvideA
{
  T GetAlphaVal(string val);
}
公共接口IProvideA,其中T:IProvideA
{
T GetAlphaVal(字符串val);
}
现在,如果我实现一个类,它使用接口作为泛型参数

public class ImplementProvideAlpha<T>:IProvideAlpha<IProvideA<T>> where T:class
{
}
public类ImplementProvideAlpha:IProvideAlpha其中T:class
{
}

可以保证通过这样的接口实现方法参数类型或返回类型

public interface IService<TRequest, TResponse> 
    where TRequest : IRequest 
    where TResponse : IResponse
{
    TResponse Execute(TRequest request);
}

public interface IRequest
{
    object Head { get; set; }
    object Body { get; set; }
}

public interface IResponse
{
    string Message { get; set; }
    bool IsErrorOccured { get; set; }
}

public class MyServiceRequest : IRequest
{
    //Interface property implementation here...
}

public class MyServiceResponse : IResponse
{
    //Interface property implementation here...
}
公共接口iSeries设备
TRequest在哪里:IRequest
在哪里响应:我响应
{
响应执行(TRequest请求);
}
公共接口IRequest
{
对象头{get;set;}
对象体{get;set;}
}
公共接口IResponse
{
字符串消息{get;set;}
布尔ISERROR发生{get;set;}
}
公共类MyServiceRequest:IRequest
{
//接口属性实现在这里。。。
}
公共类MyService响应:IResponse
{
//接口属性实现在这里。。。
}
由于IService泛型类型,下面的代码被强制实现为这样执行

public class MyService: IService<MyServiceRequest, MyServiceResponse>
{
    public MyServiceResponse Execute(MyServiceRequestrequest request)
    {
        //Bus logic here...
    }
}
公共类MyService:IService
{
公共MyServiceResponse执行(MyServiceRequestrequest请求)
{
//这里的总线逻辑。。。
}
}

我不认为
其中t:interface
是有效的代码。工作单元存储库模式传递一个类并创建一个irepository接口。像这样的东西这不完全是你的意思,但我认为Damien是正确的,其中T:接口不是有效的代码。@Damien_不相信者我指的是接口名IProvideAlpha@Budyn我的意思是,如果你打错了,编辑你的问题,纠正你的错误