public static bool TryParse(string text, out ConditionalRange <T> result) { result = null; if (string.IsNullOrWhiteSpace(text)) { return(false); } text = text.Trim().Trim('(', ')'); if (string.IsNullOrWhiteSpace(text)) { return(false); } var parts = text.Split('~').Select(p => p.Trim().Trim('?', '*')).ToArray(); if (!string.IsNullOrWhiteSpace(parts[0])) { T from; if (!Zongsoft.Common.Convert.TryConvertValue(parts[0], out from)) { return(false); } if (result == null) { result = new ConditionalRange <T>(); } result.From = from; } if (parts.Length > 1 && !string.IsNullOrWhiteSpace(parts[1])) { T to; if (!Zongsoft.Common.Convert.TryConvertValue(parts[1], out to)) { return(false); } if (result == null) { result = new ConditionalRange <T>(); } result.To = to; } //最后返回真(即使输出参数为空) return(true); }
public void Test() { var age = new ConditionalRange <byte>(100, 18); var ageText = age.ToString(); var birthdate = new ConditionalRange <DateTime>(new DateTime(1979, 6, 9), null); var birthdateText = birthdate.ToString(); var range = ConditionalRange <int> .Parse(ageText); Assert.NotNull(range); Assert.Equal(18, range.From); Assert.Equal(100, range.To); range = Common.Convert.ConvertValue <ConditionalRange <int> >(ageText); Assert.NotNull(range); Assert.Equal(18, range.From); Assert.Equal(100, range.To); }
protected bool IsDefaultValue(ConditionalConverterContext context) { if (Zongsoft.Common.TypeExtension.IsAssignableFrom(typeof(ConditionalRange), context.Type)) { return(ConditionalRange.IsEmpty(context.Value as ConditionalRange)); } if (context.Value == null || System.Convert.IsDBNull(context.Value)) { return(context.DefaultValue == null || System.Convert.IsDBNull(context.DefaultValue)); } if (context.DefaultValue == null) { return(false); } object defaultValue; return(Zongsoft.Common.Convert.TryConvertValue(context.DefaultValue, context.Value.GetType(), out defaultValue) && object.Equals(context.Value, defaultValue)); }
public static bool IsEmpty(ConditionalRange value) { return(value == null || (value.From == null && value.To == null)); }
public static bool TryParse(string text, out ConditionalRange result) { return(TryParse <string>(text, out result)); }