Reflection, drive me crazy!

Zipping my code to the maximum has made me take the hard to understand but powerful way of .NET Reflection.

This crazy ViewModel plays with DataWrappers (a concept from the Cinch Framework by Sacha Barber). I modified the framework to add a feature that makes it even better in a reflection-wise approach.

Take a look at this method of the ViewModel:

private void DataWrapperOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
            var dataWrapper = (DataWrapperBase) sender;

            if (!dataWrapper.IsValid)

            var dataValue = dataWrapper.GetPropertyValue("DataValue");
            var dataWrapperName = dataWrapper.ParentPropertyChangeArgs.PropertyName;

            var targetPropertyName = GetTargetPropertyName(dataWrapperName);

                TargetItems.SetPropertyToAll(targetPropertyName, dataValue);
            catch (TargetException ex)

It takes the DataWrapper, its current DataValue and its name. Then, translates (optionally) the name of the property (the equivalent property in the actual object of the model) and applies the value to the analogous property of each target item (TargetItems is an IEnumerable).

It can be improved even more (applying value conversion because sometimes the target and source properties may not be of the same type, for example), but it works pretty good. And the best of this is that I saved A LOT of boring cloned lines.



Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s