question

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 asked ·

How to check if the property has description attribute?

Hallo

I have an object like below and i want to get or check only the property which has description attribute. I can loop through like below but dont know how to check for the property with description attribute.


 Public Class Sample
    
 Public Property ID as integer
 <Description("Descr1")>Public Property Val1 as integer
 <Description("Descr1")>Public Property Val2 as integer
    
 End Class
    
 For Each prop In obj.GetType().GetProperties
    '//print only property with description attributes
 Next


Thanks

dotnet-visual-basic
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered ·

If the value of the <Description(…)> attribute does not matter, then try this code:

 Dim selected_properties As List(Of PropertyInfo)
    
 selected_properties =
    GetType(Sample) _
       .GetProperties() _
       .Where(Function(p) p.GetCustomAttributes().Any(Function(a) TypeOf a Is DescriptionAttribute)) _
       .ToList

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hallo Viorel, is there any perfomance difference if i loop through all the properties in object to get the list with description attribute vs getting list of properties with description attributes and then looping (like you said)?. Thanks

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 Hobbyistprogrammer-7674 ·

I think that it is better to execute the above LINQ code one time only (on initialisation or first request). It does not depend on objects. The list can be kept in a shared variable. Then reuse the list when you need it.

0 Votes 0 ·
DewayneBasnett-7583 avatar image
0 Votes"
DewayneBasnett-7583 answered ·

I'd use LINQ but it is up to you

 Imports System.ComponentModel
 Imports System.Reflection
    
 Public Class Form1
    
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Dim selProp As IEnumerable(Of PropertyInfo)
    
         selProp = From p In GetType(Sample).GetProperties()
                      From a In p.GetCustomAttributes
                      Where TypeOf a Is DescriptionAttribute
                      Select p

        'OR
    
         For Each p As PropertyInfo In GetType(Sample).GetProperties()
             For Each a As Attribute In p.GetCustomAttributes
                 If TypeOf a Is DescriptionAttribute Then
    
                 End If
             Next
         Next
     End Sub
 End Class
    
 Public Class Sample
     <Description("Name")> Public Property Name As String = "name"
     Public Property ID As Integer
     <Description("Descr1")> Public Property Val1 As Integer
     <Description("Descr2")> Public Property Val2 As Integer
     <Obsolete("Descr3", False)> Public Property Val3 As String = "FOO"
 End Class

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.