Inheritance: UniqueComponentSerializer
Esempio n. 1
0
        /// <summary>
        /// Returns a serializer that can be used to serialize and object
        /// of type <paramref name="objectType"/>.
        /// <note>
        ///     TODO: Add support for caching.
        /// </note>
        /// </summary>
        /// <param name="objectType">The type of object to be serialized.</param>
        /// <param name="ctx">The serialization context.</param>
        virtual public ISerializer Build(Type objectType, ISerializationContext ctx)
        {
            if (objectType != null)
            {
                ISerializer s = null;

                if (typeof(IICalendar).IsAssignableFrom(objectType))
                    s = new iCalendarSerializer();
                else if (typeof(ICalendarComponent).IsAssignableFrom(objectType))
                {
                    if (typeof(IEvent).IsAssignableFrom(objectType))
                        s = new EventSerializer();
                    else
                        s = new ComponentSerializer();
                }
                else if (typeof(ICalendarProperty).IsAssignableFrom(objectType))
                    s = new PropertySerializer();
                else if (typeof(ICalendarParameter).IsAssignableFrom(objectType))
                    s = new ParameterSerializer();
                else if (typeof(string).IsAssignableFrom(objectType))
                    s = new StringSerializer();
                // FIXME: remove?
                //else if (objectType.IsGenericType && typeof(IList<>).IsAssignableFrom(objectType.GetGenericTypeDefinition()))
                //    s = new GenericListSerializer(objectType);
                else if (objectType.IsEnum)
                    s = new EnumSerializer(objectType);
                else if (typeof(TimeSpan).IsAssignableFrom(objectType))
                    s = new TimeSpanSerializer();
                else if (typeof(int).IsAssignableFrom(objectType))
                    s = new IntegerSerializer();
                else if (typeof(Uri).IsAssignableFrom(objectType))
                    s = new UriSerializer();
                else if (typeof(ICalendarDataType).IsAssignableFrom(objectType))
                    s = m_DataTypeSerializerFactory.Build(objectType, ctx);
                // Default to a string serializer, which simply calls
                // ToString() on the value to serialize it.
                else
                    s = new StringSerializer();
                
                // Set the serialization context
                if (s != null)
                    s.SerializationContext = ctx;

                return s;
            }
            return null;
        }
        public void Event6()
        {
            iCalendar iCal = new iCalendar();

            Event evt = iCal.Create<Event>();
            evt.Summary = "Test event title";
            evt.Start = new iCalDateTime(2007, 4, 29);
            evt.End = evt.Start.AddDays(1);
            evt.IsAllDay = true;

            RecurrencePattern rec = new RecurrencePattern("FREQ=WEEKLY;INTERVAL=3;BYDAY=TU,FR,SU;COUNT=4");
            evt.RecurrenceRules.Add(rec);

            EventSerializer eventSerializer = new EventSerializer();

            FileStream fs = new FileStream(@"Calendars/Serialization/Event6.ics", FileMode.Create, FileAccess.Write);
            eventSerializer.Serialize(evt, fs, Encoding.UTF8);
            fs.Close();

            #pragma warning disable 0219
            iCalendar iCal1 = new iCalendar();
            #pragma warning restore 0219

            fs = new FileStream(@"Calendars/Serialization/Event6.ics", FileMode.Open, FileAccess.Read);
            Event evt1 = CalendarComponent.LoadFromStream<Event>(fs, Encoding.UTF8);
            fs.Close();

            CompareComponents(evt, evt1);
        }
        public void Event5()
        {
            iCalendar iCal = new iCalendar();

            Event evt = iCal.Create<Event>();
            evt.Summary = "Test event title";
            evt.Start = new iCalDateTime(2007, 3, 19);
            evt.Start.IsUniversalTime = true;
            evt.Duration = new TimeSpan(24, 0, 0);
            evt.Created = evt.Start.Copy<IDateTime>();
            evt.DTStamp = evt.Start.Copy<IDateTime>();
            evt.UID = "123456789";
            evt.IsAllDay = true;

            RecurrencePattern rec = new RecurrencePattern("FREQ=WEEKLY;INTERVAL=3;BYDAY=TU,FR,SU;COUNT=4");
            evt.RecurrenceRules.Add(rec);

            iCalendarSerializer serializer = new iCalendarSerializer();
            string icalString = serializer.SerializeToString(iCal);

            Assert.IsFalse(String.Empty == icalString, "iCalendarSerializer.SerializeToString() must not be empty");

            EventSerializer eventSerializer = new EventSerializer();
            string evtString = eventSerializer.SerializeToString(evt);

            var target = "BEGIN:VEVENT\r\nCREATED:20070319T000000Z\r\nDTEND;VALUE=DATE:20070320\r\nDTSTAMP:20070319T000000Z\r\nDTSTART;VALUE=DATE:20070319\r\nRRULE:FREQ=WEEKLY;INTERVAL=3;COUNT=4;BYDAY=TU,FR,SU\r\nSEQUENCE:0\r\nSUMMARY:Test event title\r\nUID:123456789\r\nEND:VEVENT\r\n";
            Assert.AreEqual(target, evtString, "ComponentBaseSerializer.SerializeToString() serialized incorrectly");

            serializer.Serialize(iCal, @"Calendars/Serialization/Event5.ics");
            SerializeTest("Event5.ics", typeof(iCalendarSerializer));
        }
Esempio n. 4
0
        /// <summary>
        /// Returns a serializer that can be used to serialize and object
        /// of type <paramref name="objectType"/>.
        /// <note>
        ///     TODO: Add support for caching.
        /// </note>
        /// </summary>
        /// <param name="objectType">The type of object to be serialized.</param>
        /// <param name="ctx">The serialization context.</param>
        virtual public ISerializer Build(Type objectType, ISerializationContext ctx)
        {
            if (objectType != null)
            {
                ISerializer s = null;

                if (typeof(IICalendar).IsAssignableFrom(objectType))
                {
                    s = new iCalendarSerializer();
                }
                else if (typeof(ICalendarComponent).IsAssignableFrom(objectType))
                {
                    if (typeof(IEvent).IsAssignableFrom(objectType))
                    {
                        s = new EventSerializer();
                    }
                    else if (typeof(IUniqueComponent).IsAssignableFrom(objectType))
                    {
                        s = new UniqueComponentSerializer();
                    }
                    else
                    {
                        s = new ComponentSerializer();
                    }
                }
                else if (typeof(ICalendarProperty).IsAssignableFrom(objectType))
                {
                    s = new PropertySerializer();
                }
                else if (typeof(ICalendarParameter).IsAssignableFrom(objectType))
                {
                    s = new ParameterSerializer();
                }
                else if (typeof(string).IsAssignableFrom(objectType))
                {
                    s = new StringSerializer();
                }
                else if (objectType.GetTypeInfo().IsEnum)
                {
                    s = new EnumSerializer(objectType);
                }
                else if (typeof(TimeSpan).IsAssignableFrom(objectType))
                {
                    s = new TimeSpanSerializer();
                }
                else if (typeof(int).IsAssignableFrom(objectType))
                {
                    s = new IntegerSerializer();
                }
                else if (typeof(Uri).IsAssignableFrom(objectType))
                {
                    s = new UriSerializer();
                }
                else if (typeof(ICalendarDataType).IsAssignableFrom(objectType))
                {
                    s = m_DataTypeSerializerFactory.Build(objectType, ctx);
                }
                // Default to a string serializer, which simply calls
                // ToString() on the value to serialize it.
                else
                {
                    s = new StringSerializer();
                }

                // Set the serialization context
                if (s != null)
                {
                    s.SerializationContext = ctx;
                }

                return(s);
            }
            return(null);
        }