I’ll make a post soon, comparing Ruby on Rails to C# with ASP.NET MVC, but this here is atomic and might be of help for one or two readers.
If you want to set and get properties in C# in a type-agnostic way, these are the functions you want to put into a library class somewhere (or, as I did, into the class that uses them):
private object getProperty(object containingObject, string propertyName) { return containingObject.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, containingObject, null); } private void setProperty(object containingObject, string propertyName, object newValue) { containingObject.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, containingObject, new object[] { newValue }); }
That’s all I will say on this subject.



March 4th, 2010 @ 15:52
Thanks exactly what I needed.
March 4th, 2010 @ 16:19
Great, good to know I’m not the only one who had this problem
July 2nd, 2010 @ 16:52
Brilliant! Exactly what I wanted. Thanks very much.
November 23rd, 2010 @ 11:08
Thanks, a lot.
September 27th, 2011 @ 18:02
That is exactly what I was looking for! Thanks! That is AWESOME!!!