接口的输入输出泛型参数怎么写

How write input and output generic parameter for interface

请帮助我使用泛型

我正在尝试使用方法创建接口,但我不明白如何为接口设置输入和输出参数

我有一个class方法

public TeachersDto[] MapToDto(Teachers[] entities, Dictionary<string, ParameterData> data)

public Teachers[] MapDto(TeachersDto dto, Dictionary<string, ParameterData> Data)

我试试

public interface ITeachersMapper<RInput, Dictionary<string, RInput2>, TOutput>
{
    TDto[] MapToDto(TEntity[] entities, Dictionary<string, TInput> data);

    TEntity[] MapDto(TDto[] dtos, Dictionary<string, TInput> data);

}

如何编写接口的输入输出泛型参数?

不清楚您是如何尝试使用此接口的,因为您的接口与您在问题中提供的实现不匹配,但我认为这就是您要寻找的。

请记住,您可以随意调用泛型参数,但 T 只是约定俗成,因此请为您的泛型参数命名,以明确它们代表什么。

此外,您不能将特定类型(如 Dictionary)与通用类型混合使用。您可以使用 where 来拥有带有类型约束的泛型类型,但很可能您只想要一个泛型类型来定义 ParameterData 而不需要泛型规范中的任何字典内容。

我将你的 类 改为 TeacherTeacherDto,因为 类 通常采用单数形式。

    public interface IEntityMapper<TEntity, TParameterData, TDto>
    {
        TDto[] MapToDto(TEntity[] entities, Dictionary<string, TParameterData> data);
        TEntity[] MapDto(TDto[] dtos, Dictionary<string, TParameterData> data);
    }
    
    public class TeacherMapper : IEntityMapper<Teacher, ParameterData, TeacherDto>
    {
        public TeacherDto[] MapToDto(Teacher[] entities, Dictionary<string, ParameterData> data)
        {
            //..mapping to DTO here
        }
        public Teacher[] MapDto(TeacherDto[] dto, Dictionary<string, ParameterData> Data)
        {
            //..mapping to domain here
        }
    }