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. 3 6251
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
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
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() );
}
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tony Tullemans |
last post by:
I am trying to write a subroutine that will examine all
the properties of a form to determine which of those are
SqlCommand objects, and then set the Connection property
of those SqlCommands to a passed-in SqlCommand object. I
can find the properties OK but am unsuccessful in trying
to change their value due to the way I have identified
those SqlCommand objects (as a PropertyInfo). Can anyone
give me a hint (or suggest a better way) as...
|
by: Hugo Leonardo |
last post by:
I'll big problem.
I have tree class:
public Class Column
{
public Column
{...}
string _Value;
public bool HasValue = false;
|
by: Bob |
last post by:
This works only when the property return type is string:
SomeObjectsPropertyInfo.SetValue(SomeObject, SomeStringValue, Nothing)
Is there a good way to try to automatically cast the string value I'm trying to
pass into SetValue as the correct type?
Bob
|
by: Charles Law |
last post by:
I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is also a minor change in
class Mypropertyinfo, which I have commented out.
When using a structure, however, the second call to GetValue returns
"Default caption". Can anyone tell me why, and how I can make this work?
<code>
Imports System
|
by: Paul |
last post by:
This part is easy:
Dim oControl As Control
oControl = Me.Page.FindControl("TextBox1")
Dim oPropertyInfo As PropertyInfo
oPropertyInfo = oControl.GetType.GetProperty("Text")
oPropertyInfo.SetValue(oControl, "Some New Text", Nothing)
| |
by: tatilou |
last post by:
Hello,
I need add new method to my properties. these methods should give me
some attributes coming from my database.
I think, to create a class with implement PropertyInfo but I don't know
how To call my custom class.
My code like this :
public abstract class Class2 : PropertyInfo //, RuntimePropertyInfo
|
by: Carlos Rodriguez |
last post by:
I have the following function in C#public void Undo(IDesignerHost host)
{
if (!this.componentName.Equals(string.Empty) && (this.member != null))
{
IContainer container1 = (IContainer)
host.GetService(typeof(IContainer));
IComponent component1 =
container1.Components;
PropertyInfo info1 =
component1.GetType().GetProperty(this.member.Name);
|
by: FourEyes |
last post by:
I've read that Reflection is slow and shouldn't be used when
performance is a concern. In my case, performance is definitely a
concern, so I was thinking that I could collect the PropertyInfo
objects that I need at initialization, and then just call
PropertyInfo.SetValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.SetValue() ? and 2) what is a
better alternative ?
I already have a hand-made solution...
|
by: damiensawyer |
last post by:
Hi,
I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo
objects. Even though they're both derived from MemberInfo, they don't
share those two methods. I've finding myself writing the following
types of structure over and over. Is there something I'm missing? Or
some 'funky' 3.5 delegate/lambda expression way of doing this?
Thanks very much in advance,
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |