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