GeekByChoiCe 152 Practically a Master Poster Featured Poster

Hey guys,

I am desperated right now. Im my application i want to export some data from an entityset to excel. So far so good.
But i want to exclude some properties. So i thought creating a custom attribute for those properties is might a good idea to make things more general.
Anyway, for some reason the Metadata get not applied at all. I've searched the web for hours but always the same examples with the same result.
I have created a small test code to make testing easier.
If someone could tell me how to solve this problem (or even have an better idea to mark properties as excluded) i would be happy.

here the code:

Attribute:

<AttributeUsage(AttributeTargets.Property, AllowMultiple:=True)>
NotInheritable Class ExcludeFromReportAttribute
    Inherits Attribute

End Class

Classes:

Partial Public Class TestClass

    Property ID As Integer

    <ExcludeFromReport()> ReadOnly Property HasError As Boolean
        Get
            Return False
        End Get
    End Property

    Property SomethingElse As String

End Class

Public Class TestClassMetadata

    <ExcludeFromReport()> Public Property ID As Integer

End Class

Public Class TestCollection

    Public Shared Col As New List(Of TestClass)


    Public Sub New()
        For i = 0 To 10
            Col.Add(New TestClass With {.ID = i, .SomethingElse = "something" & i})
        Next
    End Sub
End Class

Function:

    Public Shared Function GetHeaders(Of T)(objects As IEnumerable(Of T)) As List(Of String)
            Dim headers As New List(Of String)
            Dim properties As New List(Of PropertyInfo)(GetType(T).GetProperties)
            For x = properties.Count - 1 To 0 Step -1
                Debug.WriteLine(String.Format("{0} => {1} => {2}", properties(x).Name, properties(x).PropertyType.ToString, properties(x).GetCustomAttributes(GetType(ExcludeFromReportAttribute), False).Length.ToString))
                If properties(x).GetCustomAttributes(GetType(ExcludeFromReportAttribute), False).Length = 0 Then
                    Debug.WriteLine(String.Format("Add => {0}", properties(x).Name))
                    headers.Add(properties(x).Name)
                End If
            Next
            Return headers
        End Function

Call:

Dim x = ExcelHelpers.GetHeaders(TestCollection.Col)
Debug.WriteLine(String.Format(String.Format("headers: {0} expected = 1", x.Count)))

As you can see in the Debug output, it gets the exclude attribute from "HasError" but not from "ID" defined in the Metadata class. Anyone got an idea? Please?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.