/// <summary> /// Converts string representation of a value to its object representation. /// </summary> /// <param name="value">The value to convert.</param> /// <param name="destinationTypeFullName">The full name of the destination type.</param> /// <returns>Object representation of the string value.</returns> /// <exception cref="ArgumentNullException">destinationTypeFullName cannot be null.</exception> public static object Convert(string value, string destinationTypeFullName) { if (string.IsNullOrEmpty(destinationTypeFullName)) { throw new ArgumentNullException(nameof(destinationTypeFullName)); } string scope = TypeConverterHelper.GetScope(destinationTypeFullName); // Value types in the "System" namespace must be special cased due to a bug in the xaml compiler if (string.Equals(scope, "System", StringComparison.Ordinal)) { if (string.Equals(destinationTypeFullName, (typeof(string).FullName), StringComparison.Ordinal)) { return(value); } else if (string.Equals(destinationTypeFullName, typeof(bool).FullName, StringComparison.Ordinal)) { return(bool.Parse(value)); } else if (string.Equals(destinationTypeFullName, typeof(int).FullName, StringComparison.Ordinal)) { return(int.Parse(value, CultureInfo.InvariantCulture)); } else if (string.Equals(destinationTypeFullName, typeof(double).FullName, StringComparison.Ordinal)) { return(double.Parse(value, CultureInfo.InvariantCulture)); } } string type = TypeConverterHelper.GetType(destinationTypeFullName); string contentControlXaml = string.Format(CultureInfo.InvariantCulture, TypeConverterHelper.ContentControlFormatString, scope, type, value); ContentControl contentControl = XamlReader.Load(contentControlXaml) as ContentControl; if (contentControl != null) { return(contentControl.Content); } return(null); }
private void UpdatePropertyValue(object targetObject) { var propertyNamePaths = this.PropertyName.Path.Split('.'); var className = propertyNamePaths[0]; var propertyName = propertyNamePaths[propertyNamePaths.Length - 1]; // At this moment only works with types in Windows.UI.Xaml.Controls namespace Type targetType = typeof(Grid).Assembly.GetType("Windows.UI.Xaml.Controls." + className); var setMethod = targetType.GetRuntimeMethods().FirstOrDefault(x => x.Name == $"Set{propertyName}"); if (setMethod == null) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, "Property {0} defined by type {1} does not expose a set method and therefore cannot be modified.", propertyName, className)); } var propertyType = setMethod.GetParameters()[1].ParameterType; Exception innerException = null; try { object result = null; string valueAsString = null; TypeInfo propertyTypeInfo = propertyType.GetTypeInfo(); if (this.Value == null) { // The result can be null if the type is generic (nullable), or the default value of the type in question result = propertyTypeInfo.IsValueType ? Activator.CreateInstance(propertyType) : null; } else if (propertyTypeInfo.IsAssignableFrom(this.Value.GetType().GetTypeInfo())) { result = this.Value; } else { valueAsString = this.Value.ToString(); result = propertyTypeInfo.IsEnum ? Enum.Parse(propertyType, valueAsString, false) : TypeConverterHelper.Convert(valueAsString, propertyType.FullName); } setMethod.Invoke(null, new[] { targetObject, result }); } catch (FormatException e) { innerException = e; } catch (ArgumentException e) { innerException = e; } if (innerException != null) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, "Cannot assign value of type {0} to property {1} of type {2}. The {1} property can be assigned only values of type {2}.", this.Value != null ? this.Value.GetType().Name : "null", this.PropertyName, propertyType.Name), innerException); } }