473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dynamic properties - SetValue(), casting value to type prob

hi -

I'm trying to set Properties dynamically (web forms).

The problem is that I can't seem to set the property value based on a
string - even though I know the Type of the Property.

I seem to have to convert the string to the correct Type for the
property before I set it's value (using SetValue) - this is where I'm
having trouble.

converting the type is easy for Color, int, boolean etc - but I'm a
bit stuck on others.

is there an existing routine for converting a string to a known type?
- surely Visual Studio does this in the Forms Designer...
example:

PropertyInfo propertyInfo =
myControl.GetType().GetProperty(propertyName);
Type propertyType = propertyInfo.PropertyType;

// aaarg - need this line to do my own Type conversion
object propertyConverted = convertType(propertyValue, propertyType);

// set the property - works fine *if* convertType worked
propertyInfo.SetValue(Label1, propertyConverted, null);

private object convertType(string data, Type type)
{
if (type == typeof(bool))
return bool.Parse(data);

if (type == typeof(System.Drawing.Color))
return Color.FromName(data);

if (type.IsEnum == false)
return System.Enum.Parse(type, data, true);

if (type == typeof(int))
return System.Int32.Parse(data);

return data;
}
Nov 16 '05 #1
0 3805

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: john | last post by:
Hi,All Gurus: It is kind of complicated, please bear with me and let me know if you have any questions. Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate...
2
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
0
by: john | last post by:
Hi,All MS Tech Gurus: I need your help!!! It is the second time I post this message, I need get some feedback ASAP, Please Help!! Thanks a lot in advance. John I have a csharp method, using...
2
by: Jeff Bush | last post by:
I am trying to create a generic Command object (following the Command design pattern) that allows me to specify a generic type, an object to operate on, and most importantly, a public Property on...
5
by: Brad | last post by:
All samples related to this see to come short of being 'truly' dynamic. For instance, after creating all the code to load/save a properties value, you turn around and save it to one you KNOW...
15
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...
3
by: Matt Weaver | last post by:
I'm trying to setup a sub that would assign the properties of a webcontrol based on data in a table. I've setup a routine which looks like this: Private Sub SetObjectProperty(ByRef obj As...
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
15
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.