public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { AnimalList animals = new AnimalList(); string[] animalStrings = (value as string).Split(new char[] { '#', '$' }, StringSplitOptions.RemoveEmptyEntries); foreach (string animalString in animalStrings) { string[] animalInfo = animalString.Split(':'); AnimalInfo animal = new AnimalInfo(); animal.Name = animalInfo[0]; animal.Number = Int32.Parse(animalInfo[1]); animals.Add(animal); } return(animals); } else { throw new ArgumentException("In ConvertFrom: can not convert to List<AnimalInfo>."); } }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value is AnimalList) { AnimalList animals = value as AnimalList; StringBuilder text = new StringBuilder(); for (int i = 0; i < animals.Count; i++) { AnimalInfo animal = animals[i]; text.Append(animal.Name + ":" + animal.Number); if (i != animals.Count - 1) { text.Append("#"); } } return(text.ToString()); } else { throw new ArgumentException(string.Format("In ConvertTo: can not convert type '{0}' to string.", value.GetType().AssemblyQualifiedName)); } }