DataBinder set property

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

DataBinder has the GetPropertyValue that returns the value of a property of
an object. It does this via reflection, but is very handy when you need it.
Is there a class similar to DataBinder that does the setting of a property
value? Something like

DataBinder.SetPropertyValue(obj, "PropertyName", value);

I know I can do this with reflection, but it would be nice not having to
reinvent the wheel.

Thanks
 
There is no reinvention necessary. It still can be compressed into one
line.

obj.GetType().GetProperty( "PropertyName" ).SetValue( obj, value, null );

A little more cumbersome that your DataBinder wish command, but you can wrap
this up yourself.

myDataBinder.SetPropertyValue( obj, "PropertyName", value );

public class myDataBinder
{
public static void SetPropertyValue( object obj, string propertyName,
object value 0
{
obj.GetType().GetProperty( propertyName ).SetValue( obj, value,
null );
}
}

HTH,

bill
 
Back
Top