Setting property via Reflection
-
I want set properties to a WebControl via Reflection. The corresponding code snippet is something like this:
public void SetProperty(Control control, string propertyname, string propertyvalue)
{
Type type = control.GetType();
System.Reflection.PropertyInfo pi = type.GetProperties(propertyname);
object value = Convert.ChangeType(propertyvalue, pi.PropertyType);
pi.SetValue(control, value, null);
}It works for most cases, but for special types like Unit or Color (Width or ForeColor for example), I get an Exception. I made some ifs for a few types so that I convert/parse them specifically to the type needed. But this way the code is quite ugly and is not guaranteed to work all the time. Anyone else know a better solution?
-
I want set properties to a WebControl via Reflection. The corresponding code snippet is something like this:
public void SetProperty(Control control, string propertyname, string propertyvalue)
{
Type type = control.GetType();
System.Reflection.PropertyInfo pi = type.GetProperties(propertyname);
object value = Convert.ChangeType(propertyvalue, pi.PropertyType);
pi.SetValue(control, value, null);
}It works for most cases, but for special types like Unit or Color (Width or ForeColor for example), I get an Exception. I made some ifs for a few types so that I convert/parse them specifically to the type needed. But this way the code is quite ugly and is not guaranteed to work all the time. Anyone else know a better solution?
-
szukuro wrote:
Anyone else know a better solution?
As long as I I don't know what your context is, for example why you need reflection here, then IMO it's hard to say this is a good/bad choice or whether there is a better one.
-
It's for a hobby project, kind of a WYSIWYG editor, but with server-side features. At this state the user types in some markup like and gets a TextBox created on the same page (via Ajax).
-
You might want to see the ParseControl[^] method.
Thanks for the link, I didn't know of this method. This way I think the whole thing will be much easier to write :). Also despite this, I think my question still remains in a more general sense: How do/Can you set properties independent of their types easily (= without if and/or switch) from string input? Though now it's not really an ASP.NET question anymore..
-
Thanks for the link, I didn't know of this method. This way I think the whole thing will be much easier to write :). Also despite this, I think my question still remains in a more general sense: How do/Can you set properties independent of their types easily (= without if and/or switch) from string input? Though now it's not really an ASP.NET question anymore..
In this case, you should use the the type converter of the property to convert a string value, this way will work not just with the primitive types, but also with custom types. To get the TypeConverter[^], you can use the TypeDescriptor[^], your sample code is updated a bit like this:
public void SetProperty(Control control, string propertyname, string propertyvalue)
{
Type type = control.GetType();
System.Reflection.PropertyInfo pi = type.GetProperty(propertyname);TypeConverter converter = TypeDescriptor.GetConverter(pi.PropertyType); object value = converter.ConvertFromInvariantString(propertyvalue); //object value = Convert.ChangeType(propertyvalue, pi.PropertyType); pi.SetValue(control, value, null);
}
-
In this case, you should use the the type converter of the property to convert a string value, this way will work not just with the primitive types, but also with custom types. To get the TypeConverter[^], you can use the TypeDescriptor[^], your sample code is updated a bit like this:
public void SetProperty(Control control, string propertyname, string propertyvalue)
{
Type type = control.GetType();
System.Reflection.PropertyInfo pi = type.GetProperty(propertyname);TypeConverter converter = TypeDescriptor.GetConverter(pi.PropertyType); object value = converter.ConvertFromInvariantString(propertyvalue); //object value = Convert.ChangeType(propertyvalue, pi.PropertyType); pi.SetValue(control, value, null);
}