Posts

Posts mit dem Label "properties" werden angezeigt.

Something very much like a PropertyGrid!

I've been working on a WPF Toolkit for my pastime project CrystalWars tonight, when I stumbled upon this lovely implementation of a PropertyGrid in WPF . At first sight, and working with basic data types only, it works very smoothly. I'll have to dive into the custom section in the next few days nights. The tutorial looks pretty telling to me.

Using distinct members of anonymous object-type instances

Sometimes it may happen that you have to set property values on objects depending on whether they exist or not. I though this was feasible using interfaces, but objects can only be checked against contracted interfaces (i.e. such interfaces that occur in the object's class hierarchy), so this is no good here. An easy way to achieve this is the type.GetProperty() and type.GetProperty().SetValue() methods, as explained below. PropertyInfo pi = obj.GetType().GetProperty("Enabled"); bool enablable = (pi != null && pi.PropertyType.Equals(typeof(bool))); if (enablable) { obj.GetType().GetProperty( "Enabled" ).SetValue( obj, true, null ); } The very same thing can also be done with events, methods, fields, nested classes etc. See the following example for an event handler implementation: EventInfo ei = obj.GetType().GetEvent("Paint"); bool paintable = (ei != null); if (paintable) { PaintEventHandler pe = _Paint; obj.GetType().GetEvent("P...