C#如何在接口中使用泛型约束

C#如何在接口中使用泛型约束,c#,C#,有谁能帮我一下,下面的c#代码有什么问题吗? 我基本上是在尝试向ICommandHandler添加约束 public interface ICommand<T> where T : BaseDto{ } public abstract class BaseCommand<T> : ICommand<T> where T : BaseDto { } public class CreateAlertCommand : BaseCommand<AlertD

有谁能帮我一下,下面的c#代码有什么问题吗? 我基本上是在尝试向ICommandHandler添加约束

public interface ICommand<T> where T : BaseDto{ }

public abstract class BaseCommand<T> : ICommand<T> where T : BaseDto { }

public class CreateAlertCommand : BaseCommand<AlertDto>{}   

public interface ICommandHandler<TCommand> where TCommand : ICommand{}
公共接口ICommand,其中T:BaseDto{}
公共抽象类BaseCommand:ICommand,其中T:BaseDto{}
公共类CreateAlertCommand:BaseCommand{}
公共接口ICommandHandler,其中TCommand:ICommand{}
我在定义ICommandHandler时出错。它说
“使用泛型类型ICommand需要1个类型参数”

公共接口ICommandHandler
public interface ICommandHandler<TCommand>
    where TCommand : ICommand
{
}
其中TCommand:ICommand { }
应该是:

public interface ICommandHandler<TCommand,TDto>
    where TCommand : ICommand<TDto>
    where TDto : BaseDto
{
}
公共接口ICommandHandler
其中TCommand:ICommand
其中TDto:BaseDto
{
}
公共接口ICommandHandler
其中TCommand:ICommand
{
}
应该是:

public interface ICommandHandler<TCommand,TDto>
    where TCommand : ICommand<TDto>
    where TDto : BaseDto
{
}
公共接口ICommandHandler
其中TCommand:ICommand
其中TDto:BaseDto
{
}

您没有
ICommand
类型。您有一个
ICommand
类型。如果您想要一个非通用的
ICommand
类型,您必须创建一个。谢谢。这真的帮助我理解。只是一个小的和不相关的注释,类名“CreateAlertCommand”应该只是“AlertCommand”,因为类名应该是名词而不是动词。@Yogee类可以表示动词,在这种情况下命名是正确的。在这种情况下,这似乎是合适的,因为
…Command
类表示命令,这些命令必须是动词。@dai,你说得对。如果“CreateAlert”是命令,则名称正确。您没有
ICommand
类型。您有一个
ICommand
类型。如果您想要一个非通用的
ICommand
类型,您必须创建一个。谢谢。这真的帮助我理解。只是一个小的和不相关的注释,类名“CreateAlertCommand”应该只是“AlertCommand”,因为类名应该是名词而不是动词。@Yogee类可以表示动词,在这种情况下命名是正确的。在这种情况下,这似乎是合适的,因为
…Command
类表示命令,这些命令必须是动词。@dai,你说得对。如果“CreateAlert”是一个命令,那么名称是正确的。非常感谢。太棒了,非常感谢你。这太棒了。