Passing dynamic arguments breaks fluent interfaces

跟風遠走 提交于 2020-01-05 08:43:15

问题


I have a set of so called fluent interfaces, so I can use syntax like this:

a.With("abc").Do("this").Then("that);

Each method returns an object cast to a corresponding interface. At design time I can use Intellisense to easily navigate between API methods. However I can no longer do it if I cast one of the arguments to a dynamic:

a.With((dynamic)"abc").Do("this").Then("that);

Not only I lose Intellisense at design time, this cast affects runtime execution: all subsequent calls after With return objects of a dynamic type potentially breaking the execution logic.

What I fail to understand is why should a dynamic argument affect contracts that only use static types. If a method With is designed to return an instance of ISomeInterface and the implementation returns SomeClass (that implements ISomeInterface), why should a dynamic object used in unrelated place infect all subsequent call chain? Is there any way to prevent it?


回答1:


I believe this post by Erik Lippert answer it all:

http://blogs.msdn.com/b/ericlippert/archive/2012/10/22/a-method-group-of-one.aspx



来源:https://stackoverflow.com/questions/13384666/passing-dynamic-arguments-breaks-fluent-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!