C#如何使用带接口的泛型约束

时间:2018-03-26 15:08:37

标签: c#

任何人都可以帮我解决以下c#代码的错误吗? 我基本上是在尝试将约束添加到ICommandHandler。

RewriteCond %{REQUEST_URI} !demo1
RewriteCond %{REQUEST_URI} !^/demo1/(.*)$
RewriteCond %{REQUEST_FILENAME} !demo1
RewriteCond %{REQUEST_FILENAME} !^/demo1/(.*)$

定义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{}

1 个答案:

答案 0 :(得分:4)

public interface ICommandHandler<TCommand>
    where TCommand : ICommand
{
}

应该是:

public interface ICommandHandler<TCommand,TDto>
    where TCommand : ICommand<TDto>
    where TDto : BaseDto
{
}