C#:如何实现和使用 NotNull 和 CanBeNull 属性

时间:2022-11-09
本文介绍了C#:如何实现和使用 NotNull 和 CanBeNull 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想让程序员和我自己知道一个方法不需要 null 并且如果你确实发送 null 给它,结果不会很漂亮.

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.

Lokad 中有一个 NotNullAttribute 和一个 CanBeNullAttribute共享库,位于 Lokad.Quality 命名空间中.

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
{
}

Attribute 继承的两个空类.它们是如何使用的?您是否必须查找 xml 文档并知道它在那里?因为我尝试制作自己的属性副本并使用 Lokad 版本,但是当我尝试直接发送空值时,我没有收到任何消息.既不是来自 ReSharper,也不是来自 VS.实际上我有点期待.但是它们是如何使用的呢?如果我尝试在其中发送空值,我可以以某种方式让 VS 为我生成警告吗?或者它只是用于某种测试框架?或者?

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?

推荐答案

在中期,代码契约"(在 4.0 中)将是一个更好的答案.它们现在可用(通过 academic 或 商业 许可证),但将在 VS2010 中进行更多集成.这可以提供静态分析和运行时支持.

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.

(编辑)示例:

Contract.RequiresAlways( x != null );

就这么简单……代码契约引擎在 IL 级别工作,因此它可以分析并在构建期间或运行时调用代码时抛出警告/错误.为了向后兼容,如果您有现有的验证代码,您只需告诉它健全性检查在哪里结束,剩下的就交给它了:

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:

if ( x == null ) throw new ArgumentNullException("x");
Contract.EndContractBlock();

这篇关于C#:如何实现和使用 NotNull 和 CanBeNull 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:为什么连接空字符串有效但调用“null.ToString()"无效? 下一篇:如何强制执行空检查?

相关文章

最新文章