久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C#:如何實現(xiàn)和使用 NotNull 和 CanBeNull 屬性

C#: How to Implement and use a NotNull and CanBeNull attribute(C#:如何實現(xiàn)和使用 NotNull 和 CanBeNull 屬性)
本文介紹了C#:如何實現(xiàn)和使用 NotNull 和 CanBeNull 屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想讓程序員和我自己知道一個方法不需要 null 并且如果你確實發(fā)送 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 版本,但是當我嘗試直接發(fā)送空值時,我沒有收到任何消息.既不是來自 ReSharper,也不是來自 VS.實際上我有點期待.但是它們是如何使用的呢?如果我嘗試在其中發(fā)送空值,我可以以某種方式讓 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 中)將是一個更好的答案.它們現(xiàn)在可用(通過 academic 或 商業(yè) 許可證),但將在 VS2010 中進行更多集成.這可以提供靜態(tài)分析和運行時支持.

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 級別工作,因此它可以分析并在構建期間或運行時調用代碼時拋出警告/錯誤.為了向后兼容,如果您有現(xiàn)有的驗證代碼,您只需告訴它健全性檢查在哪里結束,剩下的就交給它了:

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#:如何實現(xiàn)和使用 NotNull 和 CanBeNull 屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 亚洲成人免费观看 | 一区二区三区视频在线播放 | 天天综合网站 | 久久久蜜桃 | 亚洲一区二区三区在线视频 | 狠狠操狠狠爱 | 久久久久黄色 | 超碰免费在线播放 | 日韩欧美一区二区三区 | 好吊妞这里只有精品 | 999精品在线 | 日韩网站在线观看 | 成人爱爱 | 精品亚洲一区二区 | 日本天天操 | 天天看天天操 | 能看的av网站| 午夜不卡视频 | 亚洲一级二级 | 在线视频一区二区三区 | 夜夜贪欢〈高h〉 | 欧美一级在线观看 | 国产毛片一级 | 免费的黄色大片 | 国产视频一 | 午夜视频一区二区三区 | 久久er99热精品一区二区 | 亚洲无av在线中文字幕 | 在线伊人网 | 亚洲亚洲人成综合网络 | 午夜在线视频观看日韩17c | 久操伊人 | 日本特级黄色片 | 欧美人与性动交α欧美精品 | 五月天激情视频 | 五月婷婷综合网 | 日韩精品久久久久久久 | 黄色一级片黄色一级片 | 国内精品一区二区三区 | 国产福利视频在线观看 | 久久大 |