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

C#:如何實現和使用 NotNull 和 CanBeNull 屬性

C#: How to Implement and use a NotNull and CanBeNull attribute(C#:如何實現和使用 NotNull 和 CanBeNull 屬性)
本文介紹了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 屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 的方法調用)
主站蜘蛛池模板: 国产精品亚洲综合 | 国产精品一区二区免费 | 亚洲色欧美另类 | 日韩在线中文字幕 | 欧美日韩亚洲视频 | 黄网免费看 | 人成在线| 国产成人亚洲精品 | 精品久久精品 | h视频在线免费看 | 亚洲欧美日韩在线不卡 | 久久av一区 | 国产精品久久久久久久久久久久 | 黄网站涩免费蜜桃网站 | 56pao在线 | 青青草av网站 | 成人亚洲片 | 日韩久久综合 | 亚洲一二三区不卡 | 91视频在线 | 天天操天天射综合网 | 97久久精品午夜一区二区 | 国产成人在线看 | 日本在线播放一区二区 | 国产在线精品区 | 国产精品久久久久无码av | 精精久久| 国产日韩一区二区三区 | 亚洲国产欧美国产综合一区 | 一区二区三区中文字幕 | 精品久久国产视频 | 91久久国产精品 | 三级成人在线 | 一级二级三级在线观看 | 一区二区三区不卡视频 | 91精品国模一区二区三区 | 亚洲精品久久久久久久久久久久久 | 成人激情免费视频 | 国产日批| 国产精品久久久久久久7电影 | 第四色狠狠 |