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. Problem getting propery values with ProperyInfo

Problem getting propery values with ProperyInfo

Scheduled Pinned Locked Moved C#
csswpftestingbeta-testinghelp
3 Posts 2 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.
  • M Offline
    M Offline
    Mark F
    wrote on last edited by
    #1

    I have a custom class TableProperties which has properties for CSS table styles. I have a CustomColor property that has a UITypeEditor to display the ColorDialog control and then convert the color value to a hex string (e.g., ffffff). When I iterate through the PropertyInfo collection the GetValue method returns the property type rather than the value.

            // Loop which displays the properties in a ListView control (Testing only).
            foreach (PropertyInfo pi in tableProperties.GetType().GetProperties())
            {
                name = pi.Name;
                ListViewItem lvi = listView1.Items.Add(name);
                theType = pi.PropertyType.ToString();
                lvi.SubItems.Add(theType);
                
                // this returns 'TblPropTest.CustomColor' rather than 'ffffff'
                value = pi.GetValue(tableProperties, null).ToString();
    
                lvi.SubItems.Add(value);
            }
    

    The PropertyGrid works fine with the custom properties. Thanks, Mark

    K 1 Reply Last reply
    0
    • M Mark F

      I have a custom class TableProperties which has properties for CSS table styles. I have a CustomColor property that has a UITypeEditor to display the ColorDialog control and then convert the color value to a hex string (e.g., ffffff). When I iterate through the PropertyInfo collection the GetValue method returns the property type rather than the value.

              // Loop which displays the properties in a ListView control (Testing only).
              foreach (PropertyInfo pi in tableProperties.GetType().GetProperties())
              {
                  name = pi.Name;
                  ListViewItem lvi = listView1.Items.Add(name);
                  theType = pi.PropertyType.ToString();
                  lvi.SubItems.Add(theType);
                  
                  // this returns 'TblPropTest.CustomColor' rather than 'ffffff'
                  value = pi.GetValue(tableProperties, null).ToString();
      
                  lvi.SubItems.Add(value);
              }
      

      The PropertyGrid works fine with the custom properties. Thanks, Mark

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      There are a few ways to fix this. The poblem lies here:

      pi.GetValue(tableProperties, null).ToString();

      The ToString() method, unless overloaded will return the type. If you can modify the TblPropTest.CustomColor code, you should overload this method. The code below adds an extension method to the Color class to output the Hex RGB value:

      public static class ColorExtensions
      {
          static string GetHex(byte value)
          {
              return value.ToString("X");
          }
      
          public static string ToRGBHexString(this System.Drawing.Color color)
          {
              return GetHex(color.R) + GetHex(color.G) + GetHex(color.B);
          }
      }
      

      This should be of help. If you cannot use this, please post the code for the TblPropTest.CustomColor type, as this is the root of the problem.

      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

      M 1 Reply Last reply
      0
      • K Keith Barrow

        There are a few ways to fix this. The poblem lies here:

        pi.GetValue(tableProperties, null).ToString();

        The ToString() method, unless overloaded will return the type. If you can modify the TblPropTest.CustomColor code, you should overload this method. The code below adds an extension method to the Color class to output the Hex RGB value:

        public static class ColorExtensions
        {
            static string GetHex(byte value)
            {
                return value.ToString("X");
            }
        
            public static string ToRGBHexString(this System.Drawing.Color color)
            {
                return GetHex(color.R) + GetHex(color.G) + GetHex(color.B);
            }
        }
        

        This should be of help. If you cannot use this, please post the code for the TblPropTest.CustomColor type, as this is the root of the problem.

        Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.

        M Offline
        M Offline
        Mark F
        wrote on last edited by
        #3

        Yes I have overloaded the ToString method. Below are the methods in my CustomColor class. My ToString method was incorrect!

            // This method is incorrect and ...,
            public new string ToString()
            {
                return string.Format("{0:X2}{1:X2}{2:X2}", \_Red, \_Green, \_Blue);
            }
        
           // Should be...
            public override string ToString()
            {
                return string.Format("{0:X2}{1:X2}{2:X2}", \_Red, \_Green, \_Blue);
            }
        

        Thanks for the help! Mark

        modified on Monday, March 1, 2010 11:53 AM

        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