public virtual object Deserialize(CalendarReader reader) { if (type.IsPrimitive || type == (typeof(byte[])) || type == typeof(Guid)) return DeserializePrimitive(reader); object o = null; if(typeof(ICalendarSerializable).IsAssignableFrom(type)) { o = Activator.CreateInstance(type, true); var serializable = o as ICalendarSerializable; serializable?.ReadCalendar(reader); } return o; }
protected virtual object DeserializePrimitive(CalendarReader reader) { object o = null; switch(Type.GetTypeCode(type)) { case TypeCode.Boolean: o = reader.Value.Equals("TRUE", StringComparison.OrdinalIgnoreCase) ? true : false; break; case TypeCode.Char: o = char.Parse(reader.Value); break; case TypeCode.SByte: o = sbyte.Parse(reader.Value, integerStyles, culture); break; case TypeCode.Byte: o = byte.Parse(reader.Value, integerStyles, culture); break; case TypeCode.Int16: o = short.Parse(reader.Value, integerStyles, culture); break; case TypeCode.UInt16: o = ushort.Parse(reader.Value, integerStyles, culture); break; case TypeCode.Int32: o = int.Parse(reader.Value, integerStyles, culture); break; case TypeCode.UInt32: o = uint.Parse(reader.Value, integerStyles, culture); break; case TypeCode.Int64: o = long.Parse(reader.Value, integerStyles, culture); break; case TypeCode.UInt64: o = ulong.Parse(reader.Value, integerStyles, culture); break; case TypeCode.Single: o = float.Parse(reader.Value, decimalStyles, culture); break; case TypeCode.Double: o = double.Parse(reader.Value, decimalStyles, culture); break; case TypeCode.Decimal: o = decimal.Parse(reader.Value, decimalStyles, culture); break; case TypeCode.String: o = reader.Value; break; default: if(type == typeof(byte[])) o = Convert.FromBase64String(reader.Value); if (type == typeof(Guid)) o = new Guid(reader.Value); break; } return o; }