问题
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