protected virtual bool PrepareMember(MemberDescriptorBase member) { var memberType = member.Type; // Remove all SyncRoot from members if (member is PropertyDescriptor && member.OriginalName == "SyncRoot" && (member.DeclaringType.Namespace ?? string.Empty).StartsWith(SystemCollectionsNamespace)) { return(false); } // Process all attributes just once instead of getting them one by one var attributes = AttributeRegistry.GetAttributes(member.MemberInfo); YamlStyleAttribute styleAttribute = null; YamlMemberAttribute memberAttribute = null; DefaultValueAttribute defaultValueAttribute = null; foreach (var attribute in attributes) { // Member is not displayed if there is a YamlIgnore attribute on it if (attribute is YamlIgnoreAttribute) { return(false); } if (attribute is YamlMemberAttribute) { memberAttribute = (YamlMemberAttribute)attribute; continue; } if (attribute is DefaultValueAttribute) { defaultValueAttribute = (DefaultValueAttribute)attribute; continue; } if (attribute is YamlStyleAttribute) { styleAttribute = (YamlStyleAttribute)attribute; continue; } var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { if (member.AlternativeNames == null) { member.AlternativeNames = new List <string>(); } if (!string.IsNullOrEmpty(yamlRemap.Name)) { member.AlternativeNames.Add(yamlRemap.Name); } } } // If the member has a set, this is a conventional assign method if (member.HasSet) { member.SerializeMemberMode = SerializeMemberMode.Content; } else { // Else we cannot only assign its content if it is a class member.SerializeMemberMode = (memberType != typeof(string) && memberType.GetTypeInfo().IsClass) || memberType.GetTypeInfo().IsInterface || type.IsAnonymous() ? SerializeMemberMode.Content : SerializeMemberMode.Never; } // If it's a private member, check it has a YamlMemberAttribute on it if (!member.IsPublic) { if (memberAttribute == null) { return(false); } } // Gets the style member.Style = styleAttribute != null ? styleAttribute.Style : YamlStyle.Any; member.Mask = 1; // Handle member attribute if (memberAttribute != null) { member.Mask = memberAttribute.Mask; if (!member.HasSet) { if (memberAttribute.SerializeMethod == SerializeMemberMode.Assign || (memberType.GetTypeInfo().IsValueType&& member.SerializeMemberMode == SerializeMemberMode.Content)) { throw new ArgumentException("{0} {1} is not writeable by {2}.".DoFormat(memberType.FullName, member.OriginalName, memberAttribute.SerializeMethod.ToString())); } } if (memberAttribute.SerializeMethod != SerializeMemberMode.Default) { member.SerializeMemberMode = memberAttribute.SerializeMethod; } member.Order = memberAttribute.Order; } if (member.SerializeMemberMode == SerializeMemberMode.Binary) { if (!memberType.IsArray) { throw new InvalidOperationException("{0} {1} of {2} is not an array. Can not be serialized as binary." .DoFormat(memberType.FullName, member.OriginalName, type.FullName)); } if (!memberType.GetElementType().IsPureValueType()) { throw new InvalidOperationException("{0} is not a pure ValueType. {1} {2} of {3} can not serialize as binary.".DoFormat(memberType.GetElementType(), memberType.FullName, member.OriginalName, type.FullName)); } } // If this member cannot be serialized, remove it from the list if (member.SerializeMemberMode == SerializeMemberMode.Never) { return(false); } // ShouldSerialize // YamlSerializeAttribute(Never) => false // ShouldSerializeSomeProperty => call it // DefaultValueAttribute(default) => compare to it // otherwise => true var shouldSerialize = type.GetMethod("ShouldSerialize" + member.OriginalName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (shouldSerialize != null && shouldSerialize.ReturnType == typeof(bool) && member.ShouldSerialize == null) { member.ShouldSerialize = obj => (bool)shouldSerialize.Invoke(obj, EmptyObjectArray); } if (defaultValueAttribute != null && member.ShouldSerialize == null && !emitDefaultValues) { object defaultValue = defaultValueAttribute.Value; Type defaultType = defaultValue == null ? null : defaultValue.GetType(); if (defaultType.IsNumeric() && defaultType != memberType) { defaultValue = memberType.CastToNumericType(defaultValue); } member.ShouldSerialize = obj => !TypeExtensions.AreEqual(defaultValue, member.Get(obj)); } if (member.ShouldSerialize == null) { member.ShouldSerialize = ShouldSerializeDefault; } if (memberAttribute != null && !string.IsNullOrEmpty(memberAttribute.Name)) { member.Name = memberAttribute.Name; } else { member.Name = NamingConvention.Convert(member.OriginalName); } return(true); }