PropertyInfo.SetValue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ThisBytes5

    PropertyInfo.SetValue

    I am trying to save/retreive some settings from a database using
    reflection an my custom attributes. The field in teh database is a
    string, but my property is a boolen. I have the following code:

    pi.SetValue(set tingsObject, GetSettingFromD b(attribute.Set tingName),
    null);


    When I run this, I receive the following error:

    Object of type 'System.String' cannot be converted to type
    'System.Boolean '.

    I've not yet figured out how to determine if the property is a Boolean
    thus have to parse the string that comes back, or if there is a way to
    use SetValue and have it do this for me.

    Any help would be appreciated.

  • josh.go@gmail.com

    #2
    Re: PropertyInfo.Se tValue

    Hi there,

    I've had this issue to, and as I recall I used a TypeConverter to
    interpret the string value to the proper type. Property's can be
    assigned TypeConverters through their attributes, so in this situation
    you'll need to implement a TypeConverter capable of converting a String
    to a Boolean value.

    Once Implemented it would look something like this ...

    <TypeConverter( GetType(PointCo nverter))> _
    Public Property MyLocation() As Point


    Hope this helps... start looking into TypeConverter first.

    Josh

    Comment

    • ThisBytes5

      #3
      Re: PropertyInfo.Se tValue

      I found that in System.Componen tModel there is a BooleanConverte r and a
      StringConverter , I have tried adding both of these to my property with
      the TypeconvertorAt tribute, but no luck.

      I have also the following code, and it works, but is not what I want as
      it is limited to ONLY the boolean conversion, or to a big IF statement
      checking the types and creating the correct convertor. I'd like to have
      it be able to use any of the type convertors based on the property.


      BooleanConverte r temp = new BooleanConverte r();
      temp.CanConvert From(typeof(str ing));
      pi.SetValue(set tings, temp.ConvertFro m(value), null);

      Given the previous post I would expected the following to have worked:

      [TypeConverter(t ypeof(BooleanCo nverter))]

      but when I call pi.SetValue I still receive the "Object of type
      'System.String' cannot be converted to type
      'System.Boolean '." exception.

      Any info on what I'm doing wrong?

      Thanks
      Wayne

      Comment

      • josh.go@gmail.com

        #4
        Re: PropertyInfo.Se tValue

        Hey Wayne,

        You are correct about the "[TypeConverter(T ypeOf(BooleanCo nverter))]"
        not working.

        I am assuming from your code snippets that you are using
        PropertyDescrip tors and the like. It seems that calling "SetValue"
        bypasses those TypeConverter attributes...so rry about that. Anyway...i
        wrote up some simple code to simulate your problem and it worked. I
        did it using PropertyDescrip tors and the BooleanConverte r.

        btw, I am using VS2003.

        Here is the code, I hope this may help you a bit. If this doesnt help
        you still, could you post some snippets of your code?

        good luck...

        //*************** ****** START CODE *************** *********\\
        using System;
        using System.Componen tModel;

        namespace CS_TypeConversi on
        {

        public class Person
        {
        private bool m_IsTall;

        [TypeConverter(" BooleanConverte r")]
        public bool IsTall
        {
        get
        {
        return m_IsTall;
        }
        set
        {
        m_IsTall = value;
        }
        }

        [STAThread]
        static void Main(string[] args)
        {
        //Create a Person "record"
        Person personRecord = new Person();
        personRecord.Is Tall = false;

        //Print original value
        Console.WriteLi ne("IsTall : " + personRecord.Is Tall.ToString() );

        //Create our converter
        BooleanConverte r bc = new BooleanConverte r();

        //Search for the property
        PropertyDescrip torCollection pdc =
        TypeDescriptor. GetProperties(p ersonRecord.Get Type());
        PropertyDescrip tor pd = pdc.Find("IsTal l", true);

        //Set the property using the converter.
        pd.SetValue(per sonRecord, bc.ConvertFromS tring("true"));

        //Print new value
        Console.WriteLi ne("IsTall : " + personRecord.Is Tall.ToString() );
        }

        }
        }

        Comment

        Working...