导致方法从 lambda 返回
我有一个流畅的验证 API,当提供的布尔结果为 false 时,它会抛出异常。这很好用。
我想对此进行改进,不是抛出异常,而是提供一些在结果为 false 时执行的功能,然后强制调用方法返回。例如,所提供的功能可能会创建一个响应,对其进行适当的初始化并返回它。
这样做可能吗? (我怀疑不是)。
解决方法似乎是初始化一个输出参数(响应),然后从调用方法返回该参数。
期望:
Ensure.That(request.IsValid(), () => new Response { Errors = request.Errors }); //I want lambda to force containing method reeturn
已实施:
Response response;
Ensure.That(request.IsValid(), r => new Response{Errors = request.Errors}, out response);
if (response.IsNotNull()) return response;
I have a fluent validation API that throws an exception when a supplied boolean result is false. This works fine.
I'd like to improve upon this by, instead of throwing an exception, providing some functionality to execute if the result is false, and then forcing the calling method to return. Example, the supplied functionality might new up a response, initialise it appropriately and return it.
Is doing this even possible? (I suspect not).
Work around seems to be something like initializing an out param (the response), and returning that from the calling method.
Desired:
Ensure.That(request.IsValid(), () => new Response { Errors = request.Errors }); //I want lambda to force containing method reeturn
As implemented:
Response response;
Ensure.That(request.IsValid(), r => new Response{Errors = request.Errors}, out response);
if (response.IsNotNull()) return response;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上,调用方法必须知道要执行此操作。如何发出信号取决于您 - 返回值、
out
参数等 - 但您不能从委托本身强制执行它,除非出现异常(异常)当然,仍然可能被抓住)。The calling method has to know to do it, basically. How you signal that is up to you - a return value, an
out
parameter etc - but you can't force it from the delegate itself other than with an exception (which could still be caught, of course).在我所知道的任何语言中,都无法控制调用者的逻辑执行路径。即使抛出异常也不会影响调用者,因为调用者可以简单地捕获它。
像这样的事情是通过 C++ 中的宏完成的,但 C# 没有提供类似宏的功能。
There is no way, in any language that I know of, to control a caller's logical execution path. Even throwing an exception does not influence a caller because the caller can simply trap it.
Things like this are done with macros in C++, but C# provides nothing like macros.
正如 Skeet 所说,你必须告诉你的方法你希望它如何返回你的数据。一个示例(对您的特定应用程序一无所知)可能是这样的:
然后您的错误方法可以优雅地处理此问题,然后继续其操作。请注意,我在 while 循环中调用了 FixThisProblem() 方法,以便您可以递归地修复是否存在多个均返回相同错误的问题。
As Skeet said, you have to tell your method how you want it return your data. One example (knowing nothing about your specific app) could be something like this:
Then your faulted method can handle this gracefully, and then continue with its operation. Note that I have the call to the FixThisProblem() method in a while loop so that you can recursively fix if there could be multiple problems that all return the same error.
不直接 - 您可以做的是抛出异常并在调用方法中捕获它。调用方法可以预期特定类型的异常并在finally 上返回。
更好的解决方案只是有一个输出参数或返回值,如果检查通过则返回。
您想要的是让一种方法针对特定实例将代码注入到另一种方法中 - 不会为每个实例化的对象生成方法 - 只是需要像成员值这样的方法。你真正要问的问题会引发一堆蠕虫,会引发数百个问题——范围、私人/公共等。我认为这是不可能的——我真的希望它不是。
Not directly - what you can do is throw an exception and catch it in the calling method. The calling method can expect that particular type of exception and return on the finally.
A better solution would just to have an out paramater or return value and return if that checks out.
What you want is to have one method inject code into another for a specific instance - methods aren't generated for objects for every object instantiated- just what's needed like member values. What you are really asking would open up a can of worms that would raise hundreds of questions - scope, private/public, etc. I don't think it's possible - I really hope it isn't.