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. How to get value from PropertyInfo

How to get value from PropertyInfo

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 3 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.
  • E Offline
    E Offline
    Etienne_123
    wrote on last edited by
    #1

    Is it possible to get the actual property value from a PropertyInfo object? I've tried the GetValue method with various parameters, but nothing works. I just get exceptions

    P 1 Reply Last reply
    0
    • E Etienne_123

      Is it possible to get the actual property value from a PropertyInfo object? I've tried the GetValue method with various parameters, but nothing works. I just get exceptions

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Etienne_123 wrote:

      Is it possible to get the actual property value from a PropertyInfo object?

      Of course it is.

      Etienne_123 wrote:

      I've tried the GetValue method with various parameters, but nothing works. I just get exceptions

      You've posted here often enough to know that you should show both your code and the exception you get. If you'd care to post your code, we can look at it - but I suspect I know what part of your problem is here; I suspect you aren't passing the instance in to retrieve the value from.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      E B 2 Replies Last reply
      0
      • P Pete OHanlon

        Etienne_123 wrote:

        Is it possible to get the actual property value from a PropertyInfo object?

        Of course it is.

        Etienne_123 wrote:

        I've tried the GetValue method with various parameters, but nothing works. I just get exceptions

        You've posted here often enough to know that you should show both your code and the exception you get. If you'd care to post your code, we can look at it - but I suspect I know what part of your problem is here; I suspect you aren't passing the instance in to retrieve the value from.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        E Offline
        E Offline
        Etienne_123
        wrote on last edited by
        #3

        This is a sample of a similar method I`m trying to use:

        private string GetValueFromProperty(PropertyInfo pInfo)
        {
        return pInfo.GetValue(this, null).ToString();
        //return pInfo.GetValue(null, null).ToString();
        //return pInfo.GetValue(pInfo, null).ToString();
        //return pInfo.GetValue(null, new object[] {}).ToString();
        }

        The lines that are commented out are other options I've tried. All of the above gives me the following error: "Object does not match target type"

        P 1 Reply Last reply
        0
        • E Etienne_123

          This is a sample of a similar method I`m trying to use:

          private string GetValueFromProperty(PropertyInfo pInfo)
          {
          return pInfo.GetValue(this, null).ToString();
          //return pInfo.GetValue(null, null).ToString();
          //return pInfo.GetValue(pInfo, null).ToString();
          //return pInfo.GetValue(null, new object[] {}).ToString();
          }

          The lines that are commented out are other options I've tried. All of the above gives me the following error: "Object does not match target type"

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          But how are you creating your pInfo?

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          E 1 Reply Last reply
          0
          • P Pete OHanlon

            But how are you creating your pInfo?

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            E Offline
            E Offline
            Etienne_123
            wrote on last edited by
            #5

            List<PropertyInfo> properties = new List<PropertyInfo>();
            properties.AddRange(someObject.GetType().GetProperties());
            var pInfo = properties.Find(prop => prop.Name.Equals("someName"));

            This allows me to retrieve the appropriate PropertyInfo object

            P 1 Reply Last reply
            0
            • E Etienne_123

              List<PropertyInfo> properties = new List<PropertyInfo>();
              properties.AddRange(someObject.GetType().GetProperties());
              var pInfo = properties.Find(prop => prop.Name.Equals("someName"));

              This allows me to retrieve the appropriate PropertyInfo object

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              And you want to retrieve the values from someObject, so use that in your GetValue call. For instance:

              pInfo.GetValue(someObject, null);

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              E 1 Reply Last reply
              0
              • P Pete OHanlon

                Etienne_123 wrote:

                Is it possible to get the actual property value from a PropertyInfo object?

                Of course it is.

                Etienne_123 wrote:

                I've tried the GetValue method with various parameters, but nothing works. I just get exceptions

                You've posted here often enough to know that you should show both your code and the exception you get. If you'd care to post your code, we can look at it - but I suspect I know what part of your problem is here; I suspect you aren't passing the instance in to retrieve the value from.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #7

                5 for correctly guessing the source of the problem before code was posted. The questioner was indeed not passing the object that he wanted to get the property value from.

                P 1 Reply Last reply
                0
                • B BobJanova

                  5 for correctly guessing the source of the problem before code was posted. The questioner was indeed not passing the object that he wanted to get the property value from.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Thanks. The psychic filter didn't let me down today ;)

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    And you want to retrieve the values from someObject, so use that in your GetValue call. For instance:

                    pInfo.GetValue(someObject, null);

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    E Offline
                    E Offline
                    Etienne_123
                    wrote on last edited by
                    #9

                    Thanks that's the one thing I didn't try, and it worked :)

                    P 1 Reply Last reply
                    0
                    • E Etienne_123

                      Thanks that's the one thing I didn't try, and it worked :)

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      No problem. It's the commonest problem with it, which was how I guessed at the top.

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                      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