public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var strValue = value as string; if (strValue == null) { throw new InvalidOperationException(); } return(SnapPointRuleCollection.Parse(strValue)); }
public static SnapPointRuleCollection Parse(string strValue) { var snapPointRuleCollection = new SnapPointRuleCollection(); var delimitedValues = strValue.Split(Separators, StringSplitOptions.RemoveEmptyEntries); foreach (var snapPointRuleStr in delimitedValues) { var hyphenIndex = snapPointRuleStr.IndexOf("-", StringComparison.OrdinalIgnoreCase); if (hyphenIndex == -1) { throw new InvalidOperationException($"Can not convert string value '{snapPointRuleStr}' to value of type SnapPointRule"); } var targetStr = snapPointRuleStr.Substring(0, hyphenIndex); var sourceStr = snapPointRuleStr.Substring(hyphenIndex + 1, snapPointRuleStr.Length - hyphenIndex - 1); SnapPoint target; SnapPoint source; if (ConvertDictionary.TryGetValue(targetStr, out target) == false) { throw new InvalidOperationException($"Can not convert string value '{targetStr}' to value of type SnapPoint"); } if (ConvertDictionary.TryGetValue(sourceStr, out source) == false) { throw new InvalidOperationException($"Can not convert string value '{sourceStr}' to value of type SnapPoint"); } snapPointRuleCollection.Add(new SnapPointRule { Target = target, Source = source }); } return(snapPointRuleCollection); }