I don't know where to start with that Reflection code. It is so wrong that it is beyond comment. To use Reflection you need to understand the Java objects and their methods and you seem not to.
I'm sorry I butchered your library. I did look at the generated Java code, but it didn't point me to the right direction, as I miss some fundamentals to use this library I think.
I don't see why you need to use Reflection at all. Cannot you just update the Map normally. You need to explain what you are trying to achieve.
Well, I don't want to update a Map. I want to use a Map to update a User instance.
In the User class in my post above, the User has 2 fields. Firstname and Lastname.
What I actually want to do, is have a JSON file, something like this:
{
"Firstname": "Matt",
"Lastname":"Jones"
}
Then I want to load this JSON to a Map. In this Map the keys will be the field names of the User class, the values will be the values that I want to set on their respective field names.
That's why I wrote this code:
Public Sub UpdateByMap(m As Map)
Dim reflect As Reflector
reflect.Target = Me '<- Set the reflector target to this user instance
For Each key As String In m.Keys '<- For each key, AKA Firstname, Lastname
reflect.SetField2("m" & key, m.Get(key)) '<- Set the field name m + key AKA mFirstname, mLastname to their respective values AKA Matt, Jones
Next
End Sub
Is what I want to do, setting fields trough a variable field name (from a JSON, Map, Whatever) possible?
Basically, I want something like CallSub, but for setting Fields, not calling subs. I hope I somewhat explained clearly what I want to achieve.
P.S. I wrote the Reflection library
Very nice! Way above my league!
Thanks for your reply!