private static void FillControlPropertyMeta(ControlPropertyEntity entity, Object model, Control control) { var modelType = model.GetType(); if (!_defComparer.Equals(entity.ClassName, modelType.Name)) { return; } var controlType = control.GetType(); var modelProperty = modelType.GetProperty(entity.ClassProperty); var controlProperty = controlType.GetProperty(entity.ControlProperty); ThrowPropertyNotFound(entity, modelType, modelProperty, controlType, controlProperty); var dictionary = (IDictionary)modelProperty.GetValue(model); if (dictionary == null) { return; } var objectValue = (Object)null; if (dictionary.Contains(entity.ClassPropertyParams)) { objectValue = dictionary[entity.ClassPropertyParams]; } var convertedValue = ConvertValue(objectValue, controlProperty.PropertyType, true); controlProperty.SetValue(control, convertedValue); }
public static void FillControlProperty(ControlPropertyEntity entity, Object model, Control control) { if (IsMetaProperty(entity)) { FillControlPropertyMeta(entity, model, control); } else { FillControlPropertySimple(entity, model, control); } }
private static void ThrowPropertyNotFound(ControlPropertyEntity entity, Type modelType, PropertyInfo modelProperty, Type controlType, PropertyInfo controlProperty) { if (modelProperty == null) { var message = String.Format("Unable to find property '{0}' of model '{1}'", entity.ClassProperty, modelType.Name); throw new Exception(message); } if (controlProperty == null) { var message = String.Format("Unable to find property '{0}' of control '{1}'", entity.ControlProperty, controlType.Name); throw new Exception(message); } }
private static void ThrowPropertyNotFound(ControlPropertyEntity entity, Type modelType, PropertyInfo modelProperty, Type controlType, PropertyInfo controlProperty) { if (modelProperty == null) { var message = $"Unable to find property '{entity.ClassProperty}' of model '{modelType.Name}'"; throw new Exception(message); } if (controlProperty == null) { var message = $"Unable to find property '{entity.ControlProperty}' of control '{controlType.Name}'"; throw new Exception(message); } }
private static void FillModelPropertyMeta(ControlPropertyEntity entity, Object model, Control control) { var modelType = model.GetType(); if (!_defComparer.Equals(entity.ClassName, modelType.Name)) { return; } var controlType = control.GetType(); var modelProperty = modelType.GetProperty(entity.ClassProperty); var controlProperty = controlType.GetProperty(entity.ControlProperty); ThrowPropertyNotFound(entity, modelType, modelProperty, controlType, controlProperty); var dictionary = (IDictionary)modelProperty.GetValue(model); if (dictionary == null) { if (modelProperty.PropertyType.IsInterface) { dictionary = new Dictionary <String, Object>(); } else { dictionary = (IDictionary)Activator.CreateInstance(modelProperty.PropertyType); } modelProperty.SetValue(model, dictionary); } var destinationType = typeof(Object); if (modelProperty.PropertyType.IsGenericType) { var genericArguments = modelProperty.PropertyType.GetGenericArguments(); destinationType = genericArguments[genericArguments.Length - 1]; } var objectValue = controlProperty.GetValue(control); var convertedValue = ConvertValue(objectValue, destinationType, UIModelSettings.DefaultIfNull); dictionary[entity.ClassPropertyParams] = convertedValue; }
private static void FillModelPropertySimple(ControlPropertyEntity entity, Object model, Control control) { var modelType = model.GetType(); if (!_defComparer.Equals(entity.ClassName, modelType.Name)) { return; } var controlType = control.GetType(); var modelProperty = modelType.GetProperty(entity.ClassProperty); var controlProperty = controlType.GetProperty(entity.ControlProperty); ThrowPropertyNotFound(entity, modelType, modelProperty, controlType, controlProperty); var objectValue = controlProperty.GetValue(control); var convertedValue = ConvertValue(objectValue, modelProperty.PropertyType, UIModelSettings.DefaultIfNull); modelProperty.SetValue(model, convertedValue); }
private static IEnumerable <ControlPropertyEntity> ParseProperties(String value) { var bindings = ParseBinding(value); foreach (var dict in bindings) { var mode = GetBindMode(dict); foreach (var pair in dict) { if (!_defComparer.Equals(pair.Key, UIModelSettings.ModeProperty)) { var source = ParseProperty(pair.Key); var bindEntity = new BindPropertyEntity(source, mode); //var target = ParseProperty(pair.Value); var controlEntity = new ControlPropertyEntity(bindEntity, pair.Value); yield return(controlEntity); } } } }
private static bool IsMetaProperty(ControlPropertyEntity entity) { return(!String.IsNullOrWhiteSpace(entity.ClassPropertyParams)); }