WriterDelegate WriteComplexElement(DynamicMember member, string name, object defaultValue, bool alwaysWrite)
 {
     EQXSerializer serializer = EQXSerializer.GetSerializer(member.MemberType);
     if (alwaysWrite)
     {
         return (XElement result, ref object target) =>
         {
             object value = member.GetValue(target) ?? defaultValue;
             if (value != null)
                 result.Add(serializer.Serialize(ref value, name));
             else
                 result.Add(new XElement(name));
         };
     }
     else
     {
         return (XElement result, ref object target) =>
         {
             object value = member.GetValue(target) ?? defaultValue;
             if (value != null)
                 result.Add(new XElement(name));
         };
     }
 }
 WriterDelegate WritePrimitiveElement(DynamicMember member, string name, object defaultValue, bool alwaysWrite)
 {
     if (alwaysWrite)
     {
         return (XElement result, ref object target) =>
         {
             object value = member.GetValue(target) ?? defaultValue ?? string.Empty;
             result.Add(new XElement(name, value));
         };
     }
     else
     {
         return (XElement result, ref object target) =>
         {
             object value = member.GetValue(target);
             if (value != defaultValue)
                 result.Add(new XElement(name, value));
         };
     }
 }
 WriterDelegate WriteArray(DynamicMember member, string name, string itemName, object defaultValue, bool alwaysWrite)
 {
     EQXSerializer serializer = EQXSerializer.GetSerializer(member.ArrayItemType);
     if (!string.IsNullOrEmpty(itemName))
     {
         if (alwaysWrite)
         {
             return (XElement parent, ref object target) =>
             {
                 XElement arrayElement = new XElement(name);
                 IList value = member.GetValue(target) as IList;
                 if (value != null && value.Count > 0)
                 {
                     for (int i = 0; i < value.Count; i++)
                     {
                         object itemValue = value[i];
                         arrayElement.Add(serializer.Serialize(ref itemValue, itemName));
                     }
                     parent.Add(arrayElement);
                 }
             };
         }
         else
         {
             return (XElement parent, ref object target) =>
             {
                 XElement arrayElement = new XElement(name);
                 IList value = member.GetValue(target) as IList;
                 if (value != null)
                 {
                     for (int i = 0; i < value.Count; i++)
                     {
                         object itemValue = value[i];
                         arrayElement.Add(serializer.Serialize(ref itemValue, itemName));
                     }
                 }
                 parent.Add(arrayElement);
             };
         }
     }
     else
     {
         // don't bother writing an empty element for flat arrays
         return (XElement parent, ref object target) =>
         {
             IList value = member.GetValue(target) as IList;
             if (value != null)
             {
                 for (int i = 0; i < value.Count; i++)
                 {
                     object itemValue = value[i];
                     parent.Add(serializer.Serialize(ref itemValue, name));
                 }
             }
         };
     }
 }