C#:如何实现和使用 NotNull 和 CanBeNull 属性
我想让程序员和我自己知道,某个方法不需要 null
,并且如果您无论如何都向它发送 null
,结果将不会很漂亮。
Lokad 共享库< 中有一个 NotNullAttribute
和 CanBeNullAttribute
/a>,位于 Lokad.Quality
命名空间中。
但这是如何运作的呢? 我查看了这两个属性的源代码,它看起来像这样:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter |
AttributeTargets.Property | AttributeTargets.Delegate |
AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
[NoCodeCoverage]
public sealed class NotNullAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter |
AttributeTargets.Property | AttributeTargets.Delegate |
AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
[NoCodeCoverage]
public sealed class CanBeNullAttribute : Attribute
{
}
两个继承自 Attribute
的空类。 它们是如何使用的? 您是否必须查找 xml 文档并知道它在那里? 因为我尝试制作自己的属性副本并使用 Lokad 版本,但是当我尝试直接发送 null 时,我没有收到任何消息。 既不是来自 ReSharper,也不是来自 VS。 实际上我有点期待。 但它们是如何使用的呢? 如果我尝试发送其中为空的内容,我可以以某种方式让 VS 为我生成警告吗? 或者它只是用于某种测试框架? 或者?
I want to let programmers and myself know that a method does not want null
and if you do send null
to it anyways, the result will not be pretty.
There is a NotNullAttribute
and a CanBeNullAttribute
in Lokad Shared Libraries, in the Lokad.Quality
namespace.
But how does that work? I looked at the source-code of those two attributes, and it looks like this:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter |
AttributeTargets.Property | AttributeTargets.Delegate |
AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
[NoCodeCoverage]
public sealed class NotNullAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter |
AttributeTargets.Property | AttributeTargets.Delegate |
AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
[NoCodeCoverage]
public sealed class CanBeNullAttribute : Attribute
{
}
Two empty classes inheriting from Attribute
. How are they used? Do you have to look up xml-documentation and know that it is there? Cause I tried to both make my own copy of the attribute and to use the Lokad version, but when I tried to send a null directly in, I got no message. Neither from ReSharper nor from VS. Which I kind of expected actually. But how are they used? Can I somehow make VS generate warnings for me if I try to send something that is null in there? Or is it just used in some kind of testing framework? Or?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从中期来看,“代码契约”(4.0 中)将是对此的更好答案。 它们现已推出(学术或商业 许可证),但将在 VS2010 中更加集成。 这可以提供静态分析和运行时支持。
(编辑)示例:
就这么简单...代码契约引擎在 IL 级别工作,因此它可以对其进行分析,并在构建期间或运行时调用代码抛出警告/错误。 为了向后兼容,如果您有现有的验证代码,您只需告诉它健全性检查在哪里结束,它就会完成剩下的工作:
In the mid-term, "code contracts" (in 4.0) will be a better answer to this. They are available now (with academic or commercial licences), but will be more integrated in VS2010. This can provide both static analysis and runtime support.
(edit) example:
Simple as that... the code contracts engine works at the IL level, so it can analyse that and throw warnings/errors from calling code during build, or at runtime. For backwards compatibility, if you have existing validation code, you can just tell it where the sanity checking ends, and it'll do the rest:
这可以通过 AOP 来完成,其中建议在运行时验证方法参数是否是null 以及是否允许 null。 请参阅 PostSharp 和 Spring .NET 用于 AOP。
至于 ReSharper,请参阅带注释的框架:
This can be done either with AOP, whereby an Advice verifies at run-time whether a method parameter is null and whether nulls are allowed. See PostSharp and Spring.NET for AOP.
As for ReSharper, see Annotated Framework:
这些注释适用于 ReSharper,并且是从 JetBrains.Annotations 命名空间复制的。 框架可以将它们放在自己的命名空间中,但是,ReSharper 不会自动选取这些注释 - 您需要告诉 ReSharper 在选项对话框中使用自定义命名空间。 选择新的命名空间后,ReSharper 的分析将选取属性并为您提供突出显示和警告。
These annotations are for ReSharper, and are copied from the JetBrains.Annotations namespace. A framework can put them in their own namespace, however, ReSharper will NOT pick up these annotations automatically - you need to tell ReSharper to use the custom namespace in the options dialog. Once you've selected the new namespace, ReSharper's analysis will pick up the attributes and give you highlights and warnings.
正如 Anton Gogolev 所指出的,可以使用 PostSharp 创建属性。(请注意,CodeContract 在主体内使用静态方法调用方法)
2013 年 2 月更新:新的 PostSharp 3.0 版本(目前处于测试版)将支持
验证参数、字段和属性
1) 文章 validate-parameters-using -attributes 已实现
它还需要具有方法边界方面的方法属性来处理参数属性。
2) 在文章的评论中,有非常相似的实现< /a> 对于非空/非空
源代码位于google code Torch /DesignByContract
3) 另一个更复杂的示例在http://badecho.com/2011/11/validating-method-parameters-with-postsharp/
As pointed by Anton Gogolev, attributes can be created using PostSharp.(note that CodeContract is using static method calls inside body of method)
UPDATE Feb 2013: new 3.0 release of PostSharp (currently in Beta) will support
Validating parameters, fields and properties
1) Article validate-parameters-using-attributes has implementation of
It also Required a method attribute with a method boundary aspect to process the parameter attributes.
2) In the comment to the article there are links to very similar implementation for NonNull/NonEmpty
The source code is located In google code Torch/DesignByContract
3) another more complicate example is described in http://badecho.com/2011/11/validating-method-parameters-with-postsharp/