public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string    id = (value as string);
            PackageId result;

            return((id != null && PackageId.TryParse(id, out result)) ? result : base.ConvertFrom(context, culture, value));
        }
예제 #2
0
        /// <summary>
        /// Tries to convert a given string into a valid target
        /// </summary>
        /// <param name="value">A string in <owner>.<namespace>.<name>@<version> format</param>
        /// <returns>True if parsing the string was successful, false otherwise</returns>
        public static bool TryParse(string value, out PackageTarget result)
        {
            result = new PackageTarget();
            StringBuilder textBuffer = new StringBuilder(value);

            for (int i = 0; i <= textBuffer.Length; i++)
            {
                string buffer;
                if (i == textBuffer.Length)
                {
                    buffer = textBuffer.ToString().Trim();
                    textBuffer.Clear();
                }
                else if (textBuffer[i] == '@')
                {
                    buffer = textBuffer.ToString(0, i).Trim();
                    textBuffer.Remove(0, i + 1);
                }
                else
                {
                    continue;
                }
                PackageId tmp; if (PackageId.TryParse(buffer, out tmp))
                {
                    result.id = tmp;
                    break;
                }
                else
                {
                    return(false);
                }
            }
            if (textBuffer.Length > 0)
            {
                result.version = PackageVersion.Create(textBuffer.ToString().Trim());
                if (!result.version.IsValid)
                {
                    return(false);
                }
            }
            return(result.id.IsValid);
        }