Define the name of a property dynamically in VB.NET

0

I have a class with a public property in it. As follows in the example:

Public Property exemplo As List(Of Uso)
        Get
            Return x_exemplo 
        End Get
        Set
            x_exemplo = Value
        End Set
    End Property

    Private x_exemplo As List(Of Uso)

I can use it quietly. However, what I need is to dynamically name it. That is, the name "example" can not be fixed. Sometimes it will be example2, other "test", etc. It is possible? Call some function or method that defines the name of the property?

In my main file, the class is called while reading a JSON file:

For Each valor In classe-principal.propriedade-da-classe-principal.propriedade-de-outra-classe.exemplo
...
Next

That is, "main-class-property", "other-class-property" are key names of the JSON file. "example" is also, but it is a dynamic name, which depending on the file, changes its name. I have already been able to identify the dynamic name of the key, I just need to use that name as the 'property name', so I can get into it and get the next json values.

    
asked by anonymous 01.12.2017 / 13:28

2 answers

1

I do not know which library you are using to deserialize JSON, but it probably offers a way to deserialize to a Dictionary . In this case you need to deserialize to a Dictionary(Of String, Object) , hence you access the property dynamically (because the dictionary key is a String ) and cast Object pro type you want.

    
08.12.2017 / 00:52
-1

Would it be something like Vb's CallByName method?

You use the method call basically by a String For example:


Sub TestCallByName1()
    'Definir a propriedade Text no TextBox1.
    CallByName(TextBox1, "Text", CallType.Set, "New Text")

    'Obter o valor da propriedade Text no TextBox1.
    MsgBox(CallByName(TextBox1, "Text", CallType.Get))

    'Chamando um método.
    CallByName(TextBox1, "Hide", CallType.Method)
End Sub

More information can be obtained from the official documentation CallByName from Microsoft

    
13.10.2018 / 18:33