How to get all public properties of a class in the order that were declared in VB.NET

Asked

Viewed 99 times

0

I am creating a library to validate the Fiscal SPED in VB.NET and to streamline the process of creating lines, each class already has its pre-defined attributes and in the order according to the documentation, as it is in the example below:

' REGISTRO 0001: ABERTURA DO BLOCO 0
Public Class Registro0001

    ' Texto fixo contendo “0001”. 
    Public Shared reg As String = "0001"

    ' Indicador de movimento:
    '   0- Bloco com dados informados;
    '   1- Bloco sem dados informados.
    Public indMov As String = ""

    ' construtor
    Public Sub New(_reg As String, _indMov As String)
        reg = _reg
        indMov = _indMov
    End Sub

    Public Overrides Function ToString() As String
        Return $"|{reg}|{indMov}|"
    End Function

End Class

Is there any way that I can take all the properties that exist within the class in the order that were declared and use the instantiated content in them within my method ToString()?

I was thinking of something close thereof but I can’t understand how.

1 answer

1


Nothing guarantees order, so you can’t do this.

If you want a lot, you can use an attribute by setting the order manually and take this value in each property to use as a sorting key.

In fact almost always when you start doing this C# is not the appropriate language for the problem. Although I think it is, but done differently. So much so that I don’t see any agility in this, on the contrary, the performance will suffer.

You need to learn suffers reflection and attributes:

  • There’s something wrong with the answer?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.