//-------------------------------------------------------------------------------------- /// <summary> /// SetValue - Set a property to a literal value /// </summary> //-------------------------------------------------------------------------------------- internal void SetValue(string propertyName, string valueText, bool throwOnMissingProperty) { var eventInfo = GetType().GetTypeInfo().GetEvent(propertyName, _publicInstance); if (eventInfo != null) { AddBinding(propertyName, valueText); return; } var propertyInfo = GetType().GetTypeInfo().GetProperty(propertyName); if (propertyInfo == null) { if (throwOnMissingProperty) { throw new ApplicationException("Cannot find property " + propertyName + " on type " + GetType().Name); } return; } // If the property is a dictionary, then we'll try to add this value, otherwise, we set the property if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.Name == "Dictionary`2") { // Break up string into separate key/Value pairs var keyValuePairs = valueText.Split('`'); foreach (var keyValuePair in keyValuePairs) { // Break up name=value into actual key and value objects var valueParts = keyValuePair.Split(new char[] { '=' }, 2); if (valueParts.Length != 2) { throw new ApplicationException("Expected format of Name=Value"); } var types = propertyInfo.PropertyType.GenericTypeArguments; var key = UIHelpers.GetValueFromText(types[0], valueParts[0]); var value = UIHelpers.GetValueFromText(types[1], valueParts[1]); // Create dictionary object if not there var dictionary = propertyInfo.GetValue(this); if (dictionary == null) { dictionary = Activator.CreateInstance(propertyInfo.PropertyType); propertyInfo.SetValue(this, dictionary); } // Add to the dictionary var add = propertyInfo.PropertyType.GetMethod("Add", types); add.Invoke(dictionary, new object[] { key, value }); } } else { propertyInfo.SetValue(this, UIHelpers.GetValueFromText(propertyInfo.PropertyType, valueText)); } }
public AlignmentTuple(string parseText) { var parts = parseText.Split(','); if (parts.Length > 2) { throw new ArgumentException("Too many parts in alignment format."); } X = !string.IsNullOrWhiteSpace(parts[0]) ? (Alignment?)UIHelpers.GetValueFromText(typeof(Alignment), parts[0]) : null; if (parts.Length == 1) { Y = X; } else { Y = !string.IsNullOrWhiteSpace(parts[1]) ? (Alignment?)UIHelpers.GetValueFromText(typeof(Alignment), parts[1]) : null; } }