简体   繁体   English

返回泛型类型的泛型方法

[英]Generic method returning generic type

I am using a restclient that accepts a type, which is later converted to the correct IRestResponse . 我正在使用一个接受类型的restclient,后来转换为正确的IRestResponse

IRestResponse<MyClassA> response = client.Execute<MyClassA>();

Since MyClassA can also be MyClassB or MyClassC I thought about making a generic method which could handle this. 由于MyClassA也可以MyClassBMyClassC我想过做这可能处理这个的通用方法。 However without any luck. 但没有任何运气。 This is my attempt: 这是我的尝试:

public interface IRestClient{ IRestResponse response = PerformExecuteClient() 公共接口IRestClient {IRestResponse response = PerformExecuteClient()

private RestResponse<T> PerformExecuteClient<T>() {
    return client.Execute<T>();
}

The compiler tells me that client.Execute<T> does not accept abstract types. 编译器告诉我client.Execute<T>不接受抽象类型。 Which makes sense, but I have no idea how else to make this method. 这是有道理的,但我不知道如何制作这种方法。 Is it possible what I am trying to achieve here? 我想在这里实现可能吗?

Additional information based on some comments. 基于一些评论的附加信息。 As Brains Mains suggested, the method is not accepting the abstract type here. 正如Brains Mains所建议的那样,这种方法不接受抽象类型。

public interface IRestClient{
    IRestResponse<T> Execute<T>(IRestRequest request) where T : new();
}

The signature is: 签名是:

IRestResponse<T> Execute<T>(IRestRequest request) where T : new();

The generic constraint of new does not allow abstract classes. new泛型约束不允许抽象类。 Instead you can: 相反,你可以:

  1. Have an interface for your classes and have a generic constraint on that. 为您的类提供一个接口,并对其有一个通用约束。
  2. Change your MyClassA to not being abstract 将您的MyClassA更改为不抽象
  3. Remove the constraint 删除约束

Me personally - I'd say the interface option - but this is opinion based :) 我个人 - 我会说接口选项 - 但这是基于意见:)

In any case for the PerformExecuteClient<T> it must follow at least the generic constraints of the Execute<T> which it executes. 在任何情况下,对于PerformExecuteClient<T>它必须至少遵循它执行的Execute<T>的通用约束。

Did you try this: 你试过这个:

private RestResponse<T> PerformExecuteClient<T>() where T : new()
{
    return client.Execute<T>();
}

As everyone pointed out here, you have to follow the interface's generic constraint to make this work. 正如大家在这里指出的那样,你必须遵循界面的通用约束才能使其工作。 If you don't, you do not respect the interface contract by passing an unexpected type. 如果不这样做,则不会通过传递意外类型来尊重接口合同。

Also where T : new() means that you can instantiate T like this new T() . 另外where T : new()意味着你可以像这个new T()一样实例化T But by definition, an abstract class can't be instantiate, that is why you get this error. 但是根据定义,抽象类不能实例化,这就是你得到这个错误的原因。

This : 这个 :

where T : new();

Means that T will be created inside of method. 意味着T将在方法内部创建。 But how can you create instance of abstract class ? 但是如何创建抽象类的实例呢? Hence an error. 因此出错了。

The Execute method on the client object could have a where T: [Something] constraint, which your generic method would also need to apply. 客户端对象上的Execute方法可能有一个T: [Something]约束,您的泛型方法也需要应用。 Look at the source code (if possible) or documentation to see if a where constraint is applied to the generic. 查看源代码(如果可能)或文档以查看where约束是否应用于泛型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM