VB.NET || How To Add & Use Custom Attributes To Class Properties Using VB.NET
The following is a module with functions which demonstrates how to add and use custom attributes for class properties using VB.NET.
The function demonstrated on this page is a generic extension method which uses reflection to get object property data transformed according to its custom attribute.
1. Custom Attribute
The example below demonstrates the use of ‘Utils.GetPropertyData‘ to get object property data transformed according to its custom attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
' Custom Attribute Imports Utils ' Create custom attribute class Public Class DateFormatAttribute Inherits System.Attribute Public Property format As DateFormat End Class ' An enum to describe the custom attribute options Public Enum DateFormat [Default] ToLongDateString ToLongTimeString ToShortDateString ToShortTimeString End Enum ' Class that has a property that uses the custom attribute Public Class User <DateFormatAttribute(format:=DateFormat.ToShortDateString)> Public Property registered As Date Public Property name As String End Class ' Declare user Dim user = New User With { .name = "Kenneth", .registered = Date.Now } ' Get the properties with the processed attribute Dim result = user.GetPropertyData ' Display Info For Each prop In result Debug.Print($"Property: {prop.Key}, Value: {prop.Value}") Next ' expected output: ' Property: registered, Value: 11/25/2020 ' Property: name, Value: Kenneth |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 25, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils Public Class DateFormatAttribute Inherits System.Attribute Public Property format As DateFormat End Class Public Enum DateFormat [Default] ToLongDateString ToLongTimeString ToShortDateString ToShortTimeString End Enum ''' <summary> ''' Returns an objects property data with the data transformed ''' according to its attributes ''' </summary> ''' <returns>The objects property data</returns> <Runtime.CompilerServices.Extension()> Public Function GetPropertyData(Of T As {Class})(source As T) As Dictionary(Of String, Object) Dim result = New Dictionary(Of String, Object) Dim flags = System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.Public Dim properties = source.GetType.GetProperties(flags) For Each prop In properties Dim key = prop.Name Dim value = If(prop.CanRead, prop.GetValue(source _ , If(prop.GetIndexParameters.Count = 1, New Object() {Nothing}, Nothing)), Nothing) ' Get custom attributes for the property Dim customAttributes = Attribute.GetCustomAttributes(prop) For Each attribute In customAttributes If attribute.GetType.Equals(GetType(DateFormatAttribute)) Then Dim dateValue = CType(value, Date) Select Case CType(attribute, DateFormatAttribute).format Case DateFormat.ToLongDateString value = dateValue.ToLongDateString Case DateFormat.ToLongTimeString value = dateValue.ToLongTimeString Case DateFormat.ToShortDateString value = dateValue.ToShortDateString Case DateFormat.ToShortTimeString value = dateValue.ToShortTimeString Case Else value = dateValue.ToString End Select End If Next result.Add(key, value) Next Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 25, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Public Module Program Public Class User <DateFormatAttribute(format:=DateFormat.ToShortDateString)> Public Property registered As Date Public Property name As String End Class Sub Main(args As String()) Try Dim user = New User With { .name = "Kenneth", .registered = Date.Now } ' Get the properties with the processed attribute Dim result = user.GetPropertyData For Each prop In result Display($"Property: {prop.Key}, Value: {prop.Value}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply