Android Question Getting property through reflection?

Discussion in 'Android Questions' started by Troberg, Apr 22, 2015.

  1. Troberg

    Troberg Well-Known Member Licensed User

    I want to get a property of an object of (semi)unknown type. By that, I meant that I don't know exactly what type the object is, but I know that it has the property.

    I've tried this, but it fails (java.lang.RuntimeException: java.lang.NoSuchFieldException: MenuPresence):

    Sub GetProperty(obj As Object, Property As StringAs Object
    Dim r As Reflector
      r.Target = obj
    Return r.GetPublicField(Property)
    End Sub
    Of course, I can do it like this:

    Sub GetProperty(obj AsObject, Property AsString) AsObject
    "get" & Property)
    End Sub
    However, that assumes that all properties have getters and setters, and are not simply public variables of the class.

    What am I doing wrong here???
  2. Erel

    Erel Administrator Staff Member Licensed User

    The actual field name is _<lower cased field name>.

    Unless the code is obfuscated. In that case the field name is random.
    Troberg likes this.
  3. LucaMs

    LucaMs Expert Licensed User

    Off topic, I know, sorry.

    Is ReturnCallSub a new b4a keyword/function? :eek:

  4. DonManfred

    DonManfred Expert Licensed User

    I dont think so. Probably a sub From troberg...
    LucaMs and lemonisdead like this.
  5. Troberg

    Troberg Well-Known Member Licensed User

    Thanks, that would have taken me a lot of time to figure out.

    Nope, much simpler than that. Sometimes when I copy-paste from the IDE to the web browser, some spaces are lost (for example, every space after "As"). Don't know why, but it happens.
    LucaMs likes this.
  6. Troberg

    Troberg Well-Known Member Licensed User

    Still don't work. I've given up on that approach now, and use CallSub on the getters and setters.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice