public override string SerializeToString(object obj)
{
ICalendarProperty prop = obj as ICalendarProperty;
if (prop != null)
{
// Don't serialize the property if the value is null
// Push this object on the serialization context.
SerializationContext.Push(prop);
IDataTypeMapper mapper = GetService<IDataTypeMapper>();
Type serializedType = mapper.GetPropertyMapping(prop);
// Get a serializer factory that we can use to serialize
// the property and parameter values
ISerializerFactory sf = GetService<ISerializerFactory>();
StringBuilder result = new StringBuilder();
if (prop.Values != null &&
prop.Values.Any())
{
foreach (object v in prop.Values)
{
// Only serialize the value to a string if it
// is non-null.
if (v != null)
{
var valueType = v.GetType();
// Use the determined type of the value if the property
// mapping didn't yield any results.
if (serializedType == null)
serializedType = valueType;
var genericListOfSerializedType = typeof(IEnumerable<>).MakeGenericType(new Type[] { serializedType });
if (genericListOfSerializedType.IsAssignableFrom(valueType))
{
// Serialize an enumerable list of properties
foreach (object value in (IEnumerable)v)
{
SerializeValue(sf, prop, serializedType, value, result);
}
}
else
{
// Serialize an individual value
SerializeValue(sf, prop, valueType, v, result);
}
}
}
}
else
{
// If there was no value, then we need to preserve an 'empty' value
result.Append(TextUtil.WrapLines(prop.Name + ":"));
}
// Pop the object off the serialization context.
SerializationContext.Pop();
return result.ToString();
}
return null;
}