void Write(XElement elem, Type t, object obj)
{
if (obj == null)
{
// For null entries in a nullable array
// See https://github.com/Particular/NServiceBus/issues/2706
if (t.IsNullableType())
{
elem.Value = "null";
}
return;
}
IEnumerable<PropertyInfo> properties;
if (!cache.typeToProperties.TryGetValue(t, out properties))
{
cache.InitType(t);
cache.typeToProperties.TryGetValue(t, out properties);
}
foreach (var prop in properties)
{
if (IsIndexedProperty(prop))
{
throw new NotSupportedException($"Type {t.FullName} contains an indexed property named {prop.Name}. Indexed properties are not supported on message types.");
}
WriteEntry(elem, prop.Name, prop.PropertyType, DelegateFactory.CreateGet(prop).Invoke(obj));
}
foreach (var field in cache.typeToFields[t])
{
WriteEntry(elem, field.Name, field.FieldType, DelegateFactory.CreateGet(field).Invoke(obj));
}
}