溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VB.NET如何自定義屬性

發(fā)布時間:2021-12-02 10:12:21 來源:億速云 閱讀:559 作者:小新 欄目:編程語言

這篇文章主要介紹了VB.NET如何自定義屬性,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

類文件:(Class1)

  1. Imports System   

  2. Imports System.Reflection   

  3. < AttributeUsage(AttributeTargets.
    All, AllowMultiple:=True, 
    Inherited:=True)> _   

  4. Public Class Class1   

  5. Inherits System.Attribute   

  6. Private FamilyName As String   

  7. Private GivenName As String   

  8. Public Sub New(ByVal Family
    Name As String)   

  9. Me.FamilyName = FamilyName   

  10. End Sub   

  11. Public Overrides Function 
    ToString() As String   

  12. Return String.Format("Author:
    {0}{1}", FamilyName, GivenName)   

  13. End Function   

  14. Public Property Family() As String   

  15. Get   

  16. Return FamilyName   

  17. End Get   

  18. Set(ByVal Value As String)   

  19. FamilyName = Value   

  20. End Set   

  21. End Property   

  22. Public Property Given() As String   

  23. Get   

  24. Return GivenName   

  25. End Get   

  26. Set(ByVal Value As String)   

  27. GivenName = Value   

  28. End Set   

  29. End Property   

  30. End Class  

使用VB.NET自定義屬性的文件(Form3.VB)

  1. < Class1("Watkins", Given:="Damien"), 
    Class1("Abrams")> _   

  2. Public Class Form3   

  3. Inherits System.Windows.Forms.Form   

  4. Private Sub Button1_Click(ByVal sender 
    As System.Object, ByVal e As System.
    EventArgs) Handles Button1.Click   

  5. Dim t As TypeType = Type.GetType
    ("WindowsApplication6.Form3")   

  6. Dim attributes As Object() = 
    t.GetCustomAttributes(True)   

  7. Console.WriteLine("Custom 
    Attributes are: ")   

  8. For Each o As Object 
    In attributes   

  9. Console.WriteLine(o)   

  10. Next   

  11. End Sub   

  12. End Class  

感謝你能夠認真閱讀完這篇文章,希望小編分享的“VB.NET如何自定義屬性”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI