Set the name of a property dynamically in VB.NET

Asked

Viewed 134 times

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 define its name dynamically. That is, the name "example" cannot be fixed. Sometimes it will be exemple2, other "test", etc. Is this possible? Calling some function or method that defines the property name?

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, "property-of-the-main-class", "property-of-another-class" are key names of the JSON file. "example" is also, however, a dynamic name, which depending on the file, changes its name. I have already been able to identify the name of the key that is dynamic, I just need to use this name as 'property name', so I can enter it and get the following json values.

2 answers

1

I don’t know which library you’re using to deserialize JSON, but it probably offers a way to deserialize for a Dictionary. In this case you need to deserialize for a Dictionary(Of String, Object), then you access the property dynamically (because the dictionary key is a String) and cast a Object for the kind you want.

-1

It would 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 found on the official website of documentation Callbyname from Microsoft

  • Hello @eleison, this link may be a good suggestion, but your answer will not be valid if one day the link crashes. In addition, it is important for the community to have content right here on the site. It would be better to include more details in your response. A summary of the content of the link would be helpful enough! Learn more about it in this item of our Community FAQ: We want answers that contain only links?

  • 1

    Thank you @Lucasduete , I apologize, because I am new to the interactions. I made the edition as you mentioned.

Browser other questions tagged

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