admin管理员组文章数量:1190353
I dont know what for it is "in" TCommand keyword in that interface where TCommand is a class with a few properies needed for handler. Is it needed? What it gives in that context ? or maybe "in" is only explicit way to say what is implicit mechanism in generics ?
public interface ICommandHandler<in TCommand> where TCommand : class, ICommand
{
Task HandleAsync(TCommand command);
}
Comment So ..it is related only for that specificCommandHandler = handler; where SpecificHandler variable can be assigned BaseHandler type ?
Additionally SpecificHandler can be used with a ... BaseCommand ?? like
specificHandler<SpecificCommand> sHandler = new SpecificHandler();
sHandler(BaseCommand) ;
?? if yes ... tell me what for ?:)
I dont know what for it is "in" TCommand keyword in that interface where TCommand is a class with a few properies needed for handler. Is it needed? What it gives in that context ? or maybe "in" is only explicit way to say what is implicit mechanism in generics ?
public interface ICommandHandler<in TCommand> where TCommand : class, ICommand
{
Task HandleAsync(TCommand command);
}
Comment So ..it is related only for that specificCommandHandler = handler; where SpecificHandler variable can be assigned BaseHandler type ?
Additionally SpecificHandler can be used with a ... BaseCommand ?? like
specificHandler<SpecificCommand> sHandler = new SpecificHandler();
sHandler(BaseCommand) ;
?? if yes ... tell me what for ?:)
Share Improve this question edited Jan 25 at 14:46 Artur asked Jan 24 at 16:38 ArturArtur 1292 silver badges11 bronze badges 1 |1 Answer
Reset to default 3Suppose you have BaseCommand
and SpecificCommand
, where SpecificCommand : BaseCommand
. With the contravariance you've specified, you could write:
ICommandHandler<BaseCommand> handler = ...; // Whatever initialization you want
// Implicit reference conversion
ICommandHandler<SpecificCommand> specificCommandHandler = handler;
Without the contravariance, that second statement wouldn't be valid, because there wouldn't be any implicit conversion from ICommandHandler<BaseCommand>
to ICommandHandler<SpecificCommand>
.
Now you don't need that if all you want is to write handler.HandleAsync(new SpecificCommand())
- but if you want to pass a handler to a method expecting an ICommandHandler<SpecificCommand>
then that implicit conversion is exactly what you want.
本文标签: ccontravariance in genericswhat for in that caseStack Overflow
版权声明:本文标题:c# - contravariance in generics - what for in that case - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738401464a2084803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
specificHandler<SpecificCommand> sHandler = new SpecificHandler();
makes no sense to me - isspecificHandler
a variable or a type? Did you meanICommandHandler<SpecificCommand>
instead ofspecificHandler<SpecificCommand>
? What doessHandler(BaseCommand);
mean, given thatBaseCommand
is a type, and you haven't mentioned theHandleAsync
method? A complete example would make it much easier to help you - please take the time to really make your question as easy to read as possible. – Jon Skeet Commented Jan 25 at 15:31