PropertyInfo.SetValue

  • Thread starter Thread starter ThisBytes5
  • Start date Start date
T

ThisBytes5

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(settingsObject, GetSettingFromDb(attribute.SettingName),
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.
 
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(PointConverter))> _
Public Property MyLocation() As Point


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

Josh
 
I found that in System.ComponentModel there is a BooleanConverter and a
StringConverter, I have tried adding both of these to my property with
the TypeconvertorAttribute, 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.


BooleanConverter temp = new BooleanConverter();
temp.CanConvertFrom(typeof(string));
pi.SetValue(settings, temp.ConvertFrom(value), null);

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

[TypeConverter(typeof(BooleanConverter))]

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
 
Hey Wayne,

You are correct about the "[TypeConverter(TypeOf(BooleanConverter))]"
not working.

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

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.ComponentModel;

namespace CS_TypeConversion
{

public class Person
{
private bool m_IsTall;

[TypeConverter("BooleanConverter")]
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.IsTall = false;

//Print original value
Console.WriteLine("IsTall : " + personRecord.IsTall.ToString());

//Create our converter
BooleanConverter bc = new BooleanConverter();

//Search for the property
PropertyDescriptorCollection pdc =
TypeDescriptor.GetProperties(personRecord.GetType());
PropertyDescriptor pd = pdc.Find("IsTall", true);

//Set the property using the converter.
pd.SetValue(personRecord, bc.ConvertFromString("true"));

//Print new value
Console.WriteLine("IsTall : " + personRecord.IsTall.ToString());
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top