简体   繁体   English

调用通用方法而不显式指定类型参数

[英]Call a generic method without explicitly specifying the type-arguments

My controller-base has the following handler: 我的控制器库具有以下处理程序:

protected ActionResult HandleRequest<TRequest, TResponse, TViewModel>(Func<TRequest, TResponse> function, TRequest request)
    where TRequest : BaseRequest
    where TResponse : BaseResponse<TViewModel> 
    where TViewModel : BaseViewModel
{
    var viewData = function(request);

    return View(viewData);
}

Which is currently called like 目前被称为

return HandleRequest<HomeIndexGetRequest, HomeIndexGetResponse, HomeIndexGetViewModel>(GetData, request);

in my controller. 在我的控制器中。 The GetData-method looks like GetData方法看起来像

private static HomeIndexGetResponse GetData(HomeIndexGetRequest request)
{
    return new HomeIndexGetResponse
        {
            ViewModel = new HomeIndexGetViewModel()
        };
}

The HomeIndexGetResponse signature is HomeIndexGetResponse签名为

public class HomeIndexGetResponse : BaseResponse<HomeIndexGetViewModel> 

And BaseResponse is setup like 而且BaseResponse的设置像

public abstract class BaseResponse<TViewModel> where TViewModel : BaseViewModel

Question: Is there any chance that I can call the HandleRequest without specifying the type-arguments explicitly? 问题:是否有可能可以在不显式指定类型参数的情况下调用HandleRequest?

return HandleRequest(GetData, request);

Playing around a bit I came up with the following solution which works as I need it. 玩了一点,我想出了以下解决方案,它可以根据需要工作。

New non-generic base class 新的非通用基类

public abstract class BaseResponse

Changed the BaseResponse to 将BaseResponse更改为

public abstract class BaseResponse<TViewModel> : BaseResponse where TViewModel : BaseViewModel

The handler changed to 处理程序更改为

protected ActionResult HandleRequest<TRequest, TResponse>(Func<TRequest, TResponse> function, TRequest request)
    where TRequest : BaseRequest
    where TResponse : BaseResponse 
{
    var viewData = function(request);

    return View(viewData);
}

without the TViewModel type-argument. 没有TViewModel类型参数。

Finally, the controller looks like this 最后,控制器看起来像这样

return HandleRequest(GetData, request);

In the action, and the GetData is implemented 在操作中,实现了GetData

private static BaseResponse GetData(HomeIndexGetRequest request)
{
    return new HomeIndexGetResponse
        {
            ViewModel = new HomeIndexGetViewModel()
        };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FakeItEasy:在不指定类型的情况下伪造对泛型方法的调用 - FakeItEasy: Fake a call to a generic method without specifying the type 从泛型抽象类调用静态方法而不指定类型 - Call static method from generic abstract class without specifying a type 通用方法,如何在不指定类型的情况下调用父方法 - Generic Methods, how to call a parent method without specifying a type 将CollectionBase子类转换为通用列表,而无需显式指定类型 - Cast a CollectionBase subclass to a generic List without specifying the type explicitly 使用多个泛型类型调用泛型方法而不指定每个泛型类型 - Call generic Method with multiple generic Types without specifying every single generic Type 使用FakeItEasy的假通用方法,无需指定类型 - Fake generic method with FakeItEasy without specifying type 没有指定类型的C#通用方法 - C# Generic Method Without Specifying Type 何时在通用C#方法调用中显式指定类型参数? - When to explicitly specify type arguments in generic C# method calls? 无法从用法中推断出方法的类型参数。 尝试显式指定类型参数 - The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly 不能从用法中推断出方法…的类型参数。 尝试显式指定类型参数 - The type arguments for method … cannot be inferred from the usage. Try specifying the type arguments explicitly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM