gpt4 book ai didi

c# - 如何正确实现泛型接口(interface)方法?

转载 作者:行者123 更新时间:2023-11-30 15:51:57 25 4
gpt4 key购买 nike

我正在尝试实现通用接口(interface)方法,但一直出现错误。我粘贴代码是为了更好地解释我想要做什么。

我想要实现的是:基于一些输入数据(SomeModelA,SomeModelB)我想获得相同的返回类型(Template)。

namespace GenericInterfacePuzzle
{
class Program
{
static void Main(string[] args)
{
var workerA = new WorkerA();
var itemsBasedOnModelA = workerA.Get(new List<SomeModelA>());

var workerB = new WorkerB();
var itemsBasedOnModelB = workerB.Get(new List<SomeModelB>());
}
}

public interface IWorker
{
Template Get<T>(List<T> someModels);
}

public class WorkerA : IWorker
{
public Template Get<SomeModelA>(List<SomeModelA> someModels)
{
ProcessModels(someModels);
return new Template(); // let's say it's based on the result of ProcessModels
}

private void ProcessModels(List<SomeModelA> models)
{
var x = models.First();
}
}

public class WorkerB : IWorker
{
public Template Get<SomeModelB>(List<SomeModelB> someModels)
{
ProcessModels(someModels);
return new Template(); // let's say it's based on the result of ProcessModels
}

private void ProcessModels(List<SomeModelB> models)
{
var x = models.First();
}
}

public class SomeModelA
{
public string Name { get; set; }
}

public class SomeModelB
{
public string Age { get; set; }
}
public class Template
{
// Irrevelant return type
}
}

我想知道我正在处理具体模型的 WorkerA/WorkerB 类级别,并基于此我想返回一个模板类实例问题是在调用 Process 的行中:

ProcessModels(someModels);

我收到一条错误消息:

Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List of SomeModelA' to 'System.Collections.Generic.List of GenericInterfacePuzzle.SomeModelA'

任何反馈都可以理解这里可能出了什么问题,以及为什么它在传递给函数时不能识别模型类。

克里斯

最佳答案

1) 您需要在接口(interface)级别定义通用参数。否则编译器不知道 T 参数:

public interface IWorker<T> where T: SomeModel
{
Template Get(List<T> someModels);
}

2) 你需要做一个约束,因为你可能不希望任何类型被赋予你的接口(interface)。最好为您的模型创建一个基类并让它们继承它:

public abstract class SomeModel { ... }    

public class SomeModelA : SomeModel
{
public string Name { get; set; }
}

public class SomeModelB : SomeModel
{
public string Age { get; set; }
}

这样一来,您就可以在实现接口(interface)的类的声明中直接指定模型(参见第 3 点)

3) 现在您需要在子类中指定哪个模型属于哪个workertype:

public class WorkerA : IWorker<SomeModelA>
{
public Template Get(List<SomeModelA> someModels)
{
ProcessModels(someModels);
return new Template(); // let's say it's based on the result of ProcessModels
}

private void ProcessModels(List<SomeModelA> models)
{
var x = models.First();
}
}

public class WorkerB : IWorker<SomeModelB>
{
public Template Get(List<SomeModelB> someModels)
{
ProcessModels(someModels);
return new Template(); // let's say it's based on the result of ProcessModels
}

private void ProcessModels(List<SomeModelB> models)
{
var x = models.First();
}
}

您还应该删除 Get 方法中的通用规范!

public Template Get<SomeModelA>(List<SomeModelA> someModels)
^
|
remove this

这在您实现接口(interface)时已经指定:

public class WorkerA : IWorker<SomeModelA>

4) 最后一件事是在 main 方法中测试:

var worker = new WorkerA();
var itemsBasedOnModelA = worker.Get(new List<SomeModelA>());

var workerB = new WorkerB();
var itemsBasedOnModelB = worker.Get(new List<SomeModelB>());
^
|
this should be [workerB]!

关于c# - 如何正确实现泛型接口(interface)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55707199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com