Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reflection - Dynamically invoking property

Reflection - Dynamically invoking property

Scheduled Pinned Locked Moved C#
tutorialquestion
19 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Ian Shlasko

    imak wrote:

    property.SetValue("Visible", false, null);

    The property in this case is not the same as the control. "property" is actually your "get" method on Form1 to RETRIEVE the control. Try this:

    Control ctl = (Control)property.GetValue(Form1, null);
    ctl.Visible = false;

    Proud to have finally moved to the A-Ark. Which one are you in?
    Author of the Guardians Saga (Sci-Fi/Fantasy novels)

    I Offline
    I Offline
    imak
    wrote on last edited by
    #7

    I get following error now Form1 is a 'type' but is used like a 'variable'

    I 1 Reply Last reply
    0
    • E Ennis Ray Lynch Jr

      What you want to do is:

      Control control = (Control)property.GetValue(obj, null);
      control.Visible = false;

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      I Offline
      I Offline
      imak
      wrote on last edited by
      #8

      What should be the obj in this case?

      E 1 Reply Last reply
      0
      • I imak

        What should be the obj in this case?

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #9

        obj is the instance of the object you are using. The one with your attributes and properties. Really, if you check the MSDN on the topic it is relatively thorough.

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

        I 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          obj is the instance of the object you are using. The one with your attributes and properties. Really, if you check the MSDN on the topic it is relatively thorough.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          I Offline
          I Offline
          imak
          wrote on last edited by
          #10

          Got it, Thanks all for your help

          1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            I rewrite my variable names when I post answers. Ie, I was almost going to use propertyInfo instead of using property but I figured it would be too confusing.

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #11

            Ok, but that doesn't explain how property is blue and null is not

            E 1 Reply Last reply
            0
            • L Lost User

              Ok, but that doesn't explain how property is blue and null is not

              E Offline
              E Offline
              Ennis Ray Lynch Jr
              wrote on last edited by
              #12

              Oh, I just looked at my post. I had no idea what you are talking about. Ask the site admins if you want to know what crazy stuff happens not me :)

              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

              L 1 Reply Last reply
              0
              • I imak

                I get following error now Form1 is a 'type' but is used like a 'variable'

                I Offline
                I Offline
                Ian Shlasko
                wrote on last edited by
                #13

                Ok, well plug in the current instance of Form1... If you're running this code inside the form, it'd just be "this"... If you're running it elsewhere, then, well, I assume you have a reference to the form, so pop that in there.

                Proud to have finally moved to the A-Ark. Which one are you in?
                Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                1 Reply Last reply
                0
                • E Ennis Ray Lynch Jr

                  How dare you beat me by seconds with the same answer! :)

                  Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                  I Offline
                  I Offline
                  Ian Shlasko
                  wrote on last edited by
                  #14

                  And I even added a (very) brief explanation... Ha! I win the prize! :-D Wait... There's no prize? :((

                  Proud to have finally moved to the A-Ark. Which one are you in?
                  Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                  1 Reply Last reply
                  0
                  • L Lost User

                    But property is not a keyword, and null is.. what happened to the colouring?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #15

                    That's what you get to see in a pre-tag without specifying the language;

                    Control control = (Control)property.GetValue(obj, null);

                    This is with the language set to C#;

                    Control control = (Control)property.GetValue(obj, null);

                    I are Troll :suss:

                    L I 2 Replies Last reply
                    0
                    • L Lost User

                      That's what you get to see in a pre-tag without specifying the language;

                      Control control = (Control)property.GetValue(obj, null);

                      This is with the language set to C#;

                      Control control = (Control)property.GetValue(obj, null);

                      I are Troll :suss:

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #16

                      Ok thanks :)

                      1 Reply Last reply
                      0
                      • L Lost User

                        That's what you get to see in a pre-tag without specifying the language;

                        Control control = (Control)property.GetValue(obj, null);

                        This is with the language set to C#;

                        Control control = (Control)property.GetValue(obj, null);

                        I are Troll :suss:

                        I Offline
                        I Offline
                        Ian Shlasko
                        wrote on last edited by
                        #17

                        It must be guessing VB instead of C... If I remember right (Been a while), in VB, you define a property as:

                        Public Property Something
                        Get
                        Return _localVariable
                        End Get
                        Set
                        _localVariable = Value
                        End Set
                        End Property

                        So "Property" is blue... Dunno why it would guess that for your code line though... You figure the "null" would be a dead giveaway that it's C/C++/C#

                        Proud to have finally moved to the A-Ark. Which one are you in?
                        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                        L 1 Reply Last reply
                        0
                        • E Ennis Ray Lynch Jr

                          Oh, I just looked at my post. I had no idea what you are talking about. Ask the site admins if you want to know what crazy stuff happens not me :)

                          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #18

                          You need to take some responsibility for the craziness too; a PRE or CODE tag accepts an optional LANG attrribute where you specify the language, and hence the applicable list of keywords. More information can be found here[^]. By default the language stuff is handled automatically when pasting in Q&A (based on code I provided here: PRE tags galore: LPCodeRecognizer[^]). So far Chris hasn't been willing or able to implement it also in the forums. There is a default, unfortunately it is C++ everywhere (and not C# as was documented somewhere), and independent of the actual forum. And then there are some bugs in the syntax colorizer, IIRC it is easily confused by quoted stuff. Conclusion: the best you can do is change <PRE> into <PRE lang="CS"> and hope for the best. :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                          1 Reply Last reply
                          0
                          • I Ian Shlasko

                            It must be guessing VB instead of C... If I remember right (Been a while), in VB, you define a property as:

                            Public Property Something
                            Get
                            Return _localVariable
                            End Get
                            Set
                            _localVariable = Value
                            End Set
                            End Property

                            So "Property" is blue... Dunno why it would guess that for your code line though... You figure the "null" would be a dead giveaway that it's C/C++/C#

                            Proud to have finally moved to the A-Ark. Which one are you in?
                            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #19

                            There is no guessing in the forums, it is just a bad default IMO. See my reply here[^]. :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups