zoukankan      html  css  js  c++  java
  • C# 泛型说明

    详细说明转  

    https://www.cnblogs.com/dotnet261010/p/9034594.html

    最近项目用到了多个参数的泛型方法,这里说明下泛型的组成:

    /// <summary>
            /// 泛型处理方法。
            /// </summary>
            /// <typeparam name="TRequestBody"></typeparam>
            /// <typeparam name="TTestParam"></typeparam>
            /// <typeparam name="TResponse"></typeparam>
            /// <param name="request"></param>
            /// <param name="tstr"></param>
            /// <param name="codeBlock"></param>
            /// <returns></returns>
            public static async Task<Response<TResponse>> Process<TRequestBody,TTestParam, TResponse>(this Request<TRequestBody> request, TTestParam tstr, Func<FlexBenefitAppContext, Task<Response<TResponse>>> codeBlock) where TRequestBody : class
            {
                var appContext = new FlexBenefitAppContext();// BuildAppContext();
                appContext.Culture = request.Header.Culture;
                appContext.ClientCode = "benefit-client-template";//get client code from request
                appContext.EmployeeId = 1;//use openId to get employee info
                return await codeBlock(appContext);
            }

    本方法中包含了3个类型参数,分别是Process<TRequestBody,TTestParam, TResponse>,在Process方法中,分别传入了这三个类型参数的参数this Request<TRequestBody> request,这里采用的是this扩展方法,所以第一个参数就是request本身, TTestParam tstr, Func<FlexBenefitAppContext, Task<Response<TResponse>>> codeBlock

    调用方法:

     public async Task<Response<EmployeeInfo>> Info([FromBody]Request<EmployeeNoParameter> request)
            {
                var response = new Response<EmployeeInfo>()
                {
                    Header = new ResponseHeader()
                    {
                        Version = 1,
                        IsSuccess = true,
                        Errors = new List<ResponseError>()
                    }
                };
           //这里的process方法调用上面定义的泛型方法
                return await request.Process("aaaa",async (appContext) =>
                {
                    using (var serviceContext = new ServiceContext(appContext))
                    {
                        var employeeInfo = await serviceContext.EmployeeService.GetEmployeeInfo(request.Body.EmployeeNo);
                        response.Body = employeeInfo;
                    }
    
                    return response;
                });
            }
  • 相关阅读:
    微信商城中使用微信支付接口获取用户地址
    微信支付开发流程
    沉默多年,重新开博
    Extjs 表单验证后,几种错误信息展示方式
    自己对Extjs的Xtemplate的忽略
    js execCommand
    支付宝支付
    C# 将短时间格式变长正常时间格式
    SortedDictionary
    sql操作
  • 原文地址:https://www.cnblogs.com/tylertang/p/11271351.html
Copyright © 2011-2022 走看看