代码合约 + Sandcastle——有什么方法可以自定义例外部分吗?
- 我正在使用代码合同版本:1.4.40602.0
- 我复制了必要的内容和转换文件
- Sandcastle 根据我的合同要求输出异常
示例代码:
public class MyClass
{
public MyClass(Object obj)
{
Contract.Requires<ArgumentNullException>(obj != null);
}
}
结果输出(在我的文档中):
| Exception | Condition |
|---------------------------------|---------------------------------|
| System.ArgumentNullException | obj == null |
这不是那个 不好,但是我想知道是否有办法自定义条件的文本?我尝试添加用户消息 Contract.Requires
,但这并没有解决任何问题。
过去,我必须为异常编写自己的 xml 文档部分。我是否需要再次这样做才能得到我需要的东西?
免责声明:由于代码合同(目前)是一个 DevLabs 项目,因此这可能会改变,但我想知道它现在是否已经可用......如果没有,我一定会建议它。
- I'm using Code Contracts ver: 1.4.40602.0
- I copied the necessary Content and Transforms files
- Sandcastle outputs the exceptions based upon my contract requirements
Example Code:
public class MyClass
{
public MyClass(Object obj)
{
Contract.Requires<ArgumentNullException>(obj != null);
}
}
Resulting output (in my documentation):
| Exception | Condition |
|---------------------------------|---------------------------------|
| System.ArgumentNullException | obj == null |
This isn't that bad, however I wonder if there is a way to customize the text of the Condition? I attempted to add a user message Contract.Requires<ArgumentNullException>(obj != null, "obj is null.");
, however this did not solve anything.
In the past I had to write my own xml documentation section for exceptions. Am I going to have to do that again to get what I need?
Disclaimer: Since Code Contracts is (currently) a DevLabs project, this could change, but I'm wondering if it's already available right now... if not, I'll be sure to suggest it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在代码契约 1.4.51019.0 中,您可以使用重载:
Requires(bool condition, string userMessage)
但是,您的消息将附加在“Precondition failed”之后,后跟不匹配的条件。如果 Sandcastle 无法识别它,我相信这不是代码合约中的错误,因为该消息对我来说是正确的。
With Code Contracts 1.4.51019.0 you may use the overload:
Requires<TException>(bool condition, string userMessage)
However, your message will be appended after "Precondition failed" followed by the unmatched condition. If Sandcastle doesn't recognize it, I believe that it's not a fault in Code Contracts, since the message appears correctly to me.