DataBinder set property

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
 
W

William F. Robertson, Jr.

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top