文章編號: 311339 - 上次校閱: 2006年12月6日 - 版次: 2.5

DefaultValueAttribute 類別的 MSDN 文件可能會造成混淆

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
全部展開 | 全部摺疊

結論

您可能會發現 DefaultValueAttribute 類別文件,在下列 MSDN 網站上令人混淆:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcomponentmodeldefaultvalueattributeclasstopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcomponentmodeldefaultvalueattributeclasstopic.asp)
此文件中聲明中特別:
成員的預設值通常是其起始值。
從這,您可能推斷如果您設定 [預設值] 屬性為屬性,[] 屬性被初始化為該值。但是,您應該將屬性的 [預設值] 屬性設定等於初始化的值。

其他相關資訊

Microsoft.NET Framework 開發人員手冊 》 中的 [Extending Metadata Using Attributes (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconextendingmetadatausingattributes.asp) ] 主題說明:
公用語言執行時間可讓您加入類似關鍵字的描述性宣告稱為來註解程式設計項目 (例如型別]、 [欄位]、 [方法],] 和 [屬性的屬性。屬性會儲存與 Microsoft.NET Framework 檔案的中繼資料,並且可以用於描述您的程式碼以執行階段,或在執行階段會影響應用程式行為。
如果屬性的屬性等於其初始化的值,您可以存取屬性的中繼資料來決定預設值。就可以使用此預設值重設對應的成員變數如有必要。您也可以撰寫程式碼產生器,使用 [預設值] 屬性來決定是否應為成員變數產生的程式碼。您必須決定是否程式碼應該會產生為成員變數來正確設定其初始值。

但是,[預設值] 屬性,並不會 導致要使用屬性的值初始化的初始值。例如,在下列的程式碼範例 IsValueSet 屬性的預設值為 True,也會被初始化為值為 True。如果未初始化 m_isValueSet 初始值就會是 False
[Visual Basic]
Imports System.ComponentModel
 
Public Class DefaultAttributeSample
    '
    ' You must still initialize your member variable to its default value; 
    ' the DefaultValue attribute does not do this.
    Private m_isValueSet As Boolean = True

    '
    ' The DefaultValue attribute should be equal to the member's initial
    ' value.
    <DefaultValueAttribute(True)> _
    Public Property IsValueSet() As Boolean
        Get
            IsValueSet = m_isValueSet
        End Get
        Set(ByVal Value As Boolean)
            m_isValueSet = Value
        End Set
    End Property

End Class
				
如需如何套用屬性,以便讓它們提供通用語言執行階段的中繼資料的詳細資訊,請參閱下列 MSDN 網站:
使用屬性擴充中繼資料
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconextendingmetadatausingattributes.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconextendingmetadatausingattributes.asp)
因為您可以在如 Visual Studio.NET 或 Visual Studio 2005 設計工具中顯示元件,元件會需要提供設計階段工具的中繼資料的屬性。

要在設計階段正確顯示您的控制項和其成員,設計階段屬性是不可或缺的因為它們提供寶貴的資訊,以視覺化設計工具。例如在下列的程式碼片段 CategoryAttribute 屬性可讓屬性瀏覽器來顯示 TextAlignment 屬性中的 [對齊方式] 類別目錄。DescriptionAttribute 屬性可讓屬性瀏覽器,以提供屬性的簡短描述,當使用者按一下它。
[Visual C#]
   [
      Category("Alignment"),
      Description("Specifies the alignment of text.")
   ]
   public ContentAlignment TextAlignment { //... }
				
[Visual Basic]
   <Category("Alignment"), _
    Description("Specifies the alignment of text.")> _
   Public Property TextAlignment As ContentAlignment
      ' ...
   End Property
				
注意: 在 Microsoft Visual C#.NET,在 Microsoft Visual Basic.NET 或 Microsoft Visual Basic 2005 中您可以參考屬性類別命名 AttributeNameAttribute 簡稱為 AttributeName 在語法中的屬性。

?考

如需詳細資訊請參閱下列 Microsoft.NET Framework 開發人員指南 》 文件:
屬性概觀
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconattributesoverview.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconattributesoverview.asp)

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Studio .NET 2003 專業版
  • Microsoft Visual Studio .NET 2002 Professional Edition
關鍵字:?
kbmt kbvs2005swept kbvs2005applies kbcompmodel kbinfo KB311339 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:311339? (http://support.microsoft.com/kb/311339/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。