VB.NET自定义属性应用方式解析

开发 后端
我们可以使用VB.NET自定义属性的文件进行具体的代码编写,完成程序中需要的特定功能。在这里就为大家详细介绍相关应用方法。

VB.NET编程语言的出现,在一定程度上改变了编程人员的编写习惯,它灵活的编写方式提高了编程效率。我们在这里就会为大家带来关于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自定义属性的具体应用方式就为大家介绍到这里。

【编辑推荐】

  1. VB.NET异常迭代捕捉代码示例
  2. VB.NET共享成员变量创建技巧解析
  3. 范例解读VB.NET获取环境变量
  4. VB.NET抓取网页相关问题解决方法
  5. VB.NET读取内存具体代码编写方法
责任编辑:曹凯 来源: blue1000.com
相关推荐

2010-01-15 15:26:46

VB.NET自定义类型

2009-10-09 15:59:41

VB.NET对象

2010-01-11 17:40:36

VB.NET相框效果

2010-01-19 13:36:16

VB.NET可选参数

2009-10-27 11:16:20

VB.NET应用框架

2010-01-20 17:41:13

VB.NET标记语句

2010-01-15 15:03:23

VB.NET对象变量声

2010-01-11 17:58:36

VB.NET压缩ZIP

2009-11-10 17:12:22

VB.NET自定义类型

2010-01-14 18:08:08

VB.NET抽象类

2010-01-12 18:12:58

VB.NET事件

2010-01-21 18:19:45

VB.NET条件编译

2010-01-22 16:07:26

VB.NET Mid函

2009-10-13 09:24:57

VB.NET Dock

2009-10-28 10:51:32

VB.NET默认属性

2009-10-14 11:15:06

VB.NET Grou

2009-10-26 15:26:37

VB.NET属性

2010-01-12 16:20:44

VB.NET类

2010-01-21 13:11:31

VB.NET安装工程

2010-01-08 14:56:18

VB.NET类定义
点赞
收藏

51CTO技术栈公众号