Custom Type Formatter on a collection

Aug 25, 2010 at 5:03 PM

I basically have a custom type called "OpeningTime"
this looks like:

    public class OpeningTime
    {
        public virtual DayOfWeek DayOfWeek { get; set; }

        public virtual int OpeningHour { get; set; }
        public virtual int OpeningMinute { get; set; }

        public virtual int ClosingHour { get; set; }
        public virtual int ClosingMinute { get; set; }
    }


In my main entity, i have:

List<OpeningTimes> OpeningTimes {get;set;}



Now.. my question is, when i use my custom type formatter, will this work on the collection of these objects?
Or.. will i need to create a "OpeningTimeCollection" type, and do it on that?

Here is what my typeFormatter looks like:

    public class OpeningTimeFormatter : ITypeFormatter
    {
        public string ToString(object value)
        {
            var x = (OpeningTime)value;

            //Day|OpeningHour|OpeningMinute|ClosingHour|ClosingMinute
            return string.Format("{0}|{1}|{2}|{3}|{4}",
                Enum.GetName(typeof(DayOfWeek), x.DayOfWeek), x.OpeningHour, x.OpeningMinute, x.ClosingHour, x.ClosingMinute);
        }

        public object ToType(string valueString, Type expected)
        {
            var split = valueString.Split('|');

            return new OpeningTime
            {
                DayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), split[0]),
                OpeningHour = int.Parse(split[1]),
                OpeningMinute = int.Parse(split[2]),
                ClosingHour = int.Parse(split[3]),
                ClosingMinute = int.Parse(split[4])
            };
        }
    }

 

 

Coordinator
Aug 25, 2010 at 6:05 PM

Yes, this should work as you're expecting. Any property type that implements ICollection<T> is stored and formatted as a multi-valued attribute.

This page on the doc wiki outlines all the collection and array mapping rules.