問(wèn)題描述
在 Visual Studio 中,我可以選擇將警告視為錯(cuò)誤"選項(xiàng),以防止在出現(xiàn)任何警告時(shí)編譯我的代碼.我們的團(tuán)隊(duì)使用此選項(xiàng),但我們希望保留兩個(gè)警告作為警告.
有一個(gè)禁止警告的選項(xiàng),但我們確實(shí)希望它們顯示為警告,所以這不起作用.
似乎獲得我們想要的行為的唯一方法是在特定警告"文本框中輸入每個(gè) C# 警告編號(hào)的列表,除了我們希望視為警告的兩個(gè).
除了令人頭疼的維護(hù)之外,這種方法的最大缺點(diǎn)是一些警告沒(méi)有數(shù)字,因此無(wú)法明確引用.例如,無(wú)法解析此引用.找不到程序集 'Data....'"
有人知道更好的方法嗎?
<小時(shí)>為那些沒(méi)有立即明白為什么這是有用的人澄清.想想大多數(shù)警告是如何工作的.他們告訴你,你剛寫(xiě)的代碼有些不對(duì)勁.修復(fù)它們大約需要 10 秒,這使代碼庫(kù)更加整潔.
過(guò)時(shí)"警告與此大不相同.有時(shí)修復(fù)它意味著只使用一個(gè)新的方法簽名.但是,如果整個(gè)類已經(jīng)過(guò)時(shí),并且您在數(shù)十萬(wàn)行代碼中分散使用它,則可能需要數(shù)周或更長(zhǎng)時(shí)間才能修復(fù).您不希望構(gòu)建被破壞那么久,但您肯定希望看到有關(guān)它的警告.這不僅僅是一個(gè)假設(shè)的案例——這已經(jīng)發(fā)生在我們身上.
文字#warning"警告也是獨(dú)一無(wú)二的.我經(jīng)常想要檢查它,但我不想破壞構(gòu)建.
在 Visual Studio 2022 中,我們有一個(gè)新的項(xiàng)目屬性 UI,其中包括一個(gè)編輯器.
在構(gòu)建下 |錯(cuò)誤和警告 如果您將 Treat warnings as errors 設(shè)置為 All,則會(huì)出現(xiàn)另一個(gè)屬性,允許您免除特定警告被視為錯(cuò)誤:p>
這會(huì)將以下屬性添加到您的項(xiàng)目中:
<WarningsNotAsErrors>618,1030,1701,1702</WarningsNotAsErrors>
In Visual Studio, I can select the "Treat warnings as errors" option to prevent my code from compiling if there are any warnings. Our team uses this option, but there are two warnings we would like to keep as warnings.
There is an option to suppress warnings, but we DO want them to show up as warnings, so that won't work.
It appears that the only way to get the behavior we want is to enter a list of every C# warning number into the "Specific warnings" text box, except for the two we want treated as warnings.
Besides the maintenance headache, the biggest disadvantage to this approach is that a few warnings do not have numbers, so they can't be referenced explicitly. For example, "Could not resolve this reference. Could not locate assembly 'Data....'"
Does anyone know of a better way to do this?
Clarifying for those who don't see immediately why this is useful. Think about how most warnings work. They tell you something is a little off in the code you just wrote. It takes about 10 seconds to fix them, and that keeps the code base cleaner.
The "Obsolete" warning is very different from this. Sometimes fixing it means just consuming a new method signature. But if an entire class is obsolete, and you have usage of it scattered through hundreds of thousands of lines of code, it could take weeks or more to fix. You don't want the build to be broken for that long, but you definitely DO want to see a warning about it. This isn't just a hypothetical case--this has happened to us.
Literal "#warning" warnings are also unique. I often want to check it in, but I don't want to break the build.
In Visual Studio 2022 we have a new Project Properties UI which includes an editor for this.
Under Build | Errors and Warnings if you set Treat warnings as errors to All, then another property appears which allows you to exempt specific warnings from being treated as errors:
This will add the following property to your project:
<WarningsNotAsErrors>618,1030,1701,1702</WarningsNotAsErrors>
這篇關(guān)于“將所有警告視為錯(cuò)誤,除了..."在 Visual Studio 中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!