同事報案,專案有個類別 .cs 的圖示怪怪的,一般 .cs 的圖示應是綠色的 C#,但問題類別卻是個沒見過的文件圖示(下圖黃框處),比對發現是該類別繼承 System.Net.WebClient 造成(註:這麼做是為了修改 WebClient 的 Timeout),隨便新增一個 ClassN.cs,只要繼承 WebClient 圖示馬上變掉,取消繼承就會恢復。

為了搞懂奇怪圖示的意義,我開始搜尋有沒有 Visual Studio Solution Explorer 圖示大全這種東西,爬文很久發現官方文件沒提供這種東西,最後是在 VS Image Library 找到線索,順便整理過程蒐集到的參考資料:

在 Visaul Studio 2015 圖示清單文件(PDF)中比對到這個奇怪圖示,它的名字叫 Component File:

由圖示知道跟 Component 有關,回頭查 Visual Studio 發現我繞了遠路,其實 .csproj 裡就有線索,當 .cs 繼承 WebClient ,該類別 .cs 會多出 <SubType>Component</SubType>:

  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Class2.cs">
      <SubType>Component</SubType>
    </Compile>
    <Compile Include="Class3.cs" />

即便手動刪掉,開啟或修改該類別後會再自己長出來。更進一步,Visual Studio 還會為它加上特殊設計檢視:

由蒐集到的關鍵字爬文,在 Stackoverflow 找到完整解釋

會加上 <SubType>Component</SubType> 是因為 System.Net.WebClient 繼承了 System.ComponentModel.Component,修改圖示及套用特殊設計 UI 是 Visaul Studio 針對 System.ComponentModel.Component 所加入的邏輯。要避免可在類別加註 [System.ComponentModel.DesignerCategory("Code")]。

經實測,指定 DesignCategoryAttribute 即可恢復正常。

又學到冷知識。

Found the class which inherited WebClient showed different icon with other .cs file in VS2017. I found the list of VS item icon meaning and trivia of "component file" and component subtype.


Comments

Be the first to post a comment

Post a comment