Ejemplo n.º 1
0
        static Exception ValidatePropertyText(SettingsProperty property, string text)
        {
            try
            {
                if (property.SerializeAs == SettingsSerializeAs.String)
                {
                    var converter = TypeDescriptor.GetConverter(property.PropertyType);
                    if (converter != null && converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)))
                    {
                        converter.ConvertFromInvariantString(text);
                        return null;
                    }
                }
                else
                {
                    using (var reader = new StringReader(text))
                    {
                        var obj = new XmlSerializer(property.PropertyType).Deserialize(reader);
                        if (obj == null || property.PropertyType.IsAssignableFrom(obj.GetType()))
                            return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return ex;
            }

            return new NotSupportedException("Cannot convert.");
        }