Example #1
0
        protected override void OnFieldNameChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Field can be either a body member or a message header.
            // First see if the field is a body member.
            CodeAttributeDeclaration bodyMember = memberExtension.FindAttribute("System.ServiceModel.MessageBodyMemberAttribute");

            // If this is a body member, modify the MessageBodyMemberAttribute to include the wire name.
            if (bodyMember != null)
            {
                CodeAttributeDeclaration newBodyMember =
                    new CodeAttributeDeclaration("System.ServiceModel.MessageBodyMemberAttribute",
                                                 new CodeAttributeArgumentExtended("Name",
                                                                                   new CodePrimitiveExpression(oldName), true));

                memberExtension.AddAttribute(newBodyMember);
            }

            // Now check whether the field is a message header.
            CodeAttributeDeclaration header = memberExtension.FindAttribute("System.ServiceModel.MessageHeaderAttribute");

            if (header != null)
            {
                CodeAttributeDeclaration newHeader =
                    new CodeAttributeDeclaration("System.ServiceModel.MessageHeaderAttribute",
                                                 new CodeAttributeArgumentExtended("Name",
                                                                                   new CodePrimitiveExpression(oldName), true));

                memberExtension.AddAttribute(newHeader);
            }

            // Make sure the field name change is reflected in the field name references.
            ConvertFieldReferencesInConstructors(memberExtension.Parent.Constructors, oldName, newName);
        }
        protected override void OnFieldNameChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Field can be either a body member or a message header.
            // First see if the field is a body member.
            CodeAttributeDeclaration bodyMember = memberExtension.FindAttribute("System.ServiceModel.MessageBodyMemberAttribute");
            // If this is a body member, modify the MessageBodyMemberAttribute to include the wire name.
            if (bodyMember != null)
            {                
                CodeAttributeDeclaration newBodyMember =
                new CodeAttributeDeclaration("System.ServiceModel.MessageBodyMemberAttribute",
                new CodeAttributeArgumentExtended("Name",
                new CodePrimitiveExpression(oldName), true));

                memberExtension.AddAttribute(newBodyMember);                
            }

            // Now check whether the field is a message header.
            CodeAttributeDeclaration header = memberExtension.FindAttribute("System.ServiceModel.MessageHeaderAttribute");
            if (header != null)
            {
                CodeAttributeDeclaration newHeader =
                new CodeAttributeDeclaration("System.ServiceModel.MessageHeaderAttribute",
                new CodeAttributeArgumentExtended("Name",
                new CodePrimitiveExpression(oldName), true));

                memberExtension.AddAttribute(newHeader);                
            }  
            
            // Make sure the field name change is reflected in the field name references.
            ConvertFieldReferencesInConstructors(memberExtension.Parent.Constructors, oldName, newName);
        }
Example #3
0
 /// <summary>
 /// This method checks whether a given CodeTypeMember contains any attributes that will
 /// prevent from converting its type from an array to a collection.
 /// </summary>
 private bool HasInvalidAttributes(CodeTypeMemberExtension memberExtension)
 {
     if (memberExtension.FindAttribute("System.Xml.Serialization.XmlChoiceIdentifierAttribute") != null)
     {
         return(true);
     }
     else if (memberExtension.FindAttribute("System.Xml.Serialization.XmlIgnoreAttribute") != null)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #4
0
 protected override bool CanConvertMember(CodeTypeMemberExtension memberExtension)
 {
     // We can only convert the fields.
     if (memberExtension.Kind == CodeTypeMemberKind.Field)
     {
         if (memberExtension.FindAttribute("System.ServiceModel.MessageBodyMemberAttribute") != null)
         {
             return(true);
         }
         if (memberExtension.FindAttribute("System.ServiceModel.MessageHeaderAttribute") != null)
         {
             return(true);
         }
     }
     return(false);
 }
 protected override bool CanConvertMember(CodeTypeMemberExtension memberExtension)
 {
     // We can only convert the fields.
     if (memberExtension.Kind == CodeTypeMemberKind.Field)
     {
         if (memberExtension.FindAttribute("System.ServiceModel.MessageBodyMemberAttribute") != null)
         {
             return true;
         }
         if (memberExtension.FindAttribute("System.ServiceModel.MessageHeaderAttribute") != null)
         {
             return true;
         }
     }
     return false;
 }
Example #6
0
        private void OnFieldOrPropertyNameChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Here we basically have two cases. Array and non-array.
            // If it's an non-array type, we have to decorate it with either
            // XmlAttributeAttribute attribute or XmlElementAttribute attribute.
            // If it's an array type we have to decorate it with XmlArrayAttribute
            // attribute.

            // There is one quickie we can try before anything nevertheless.
            // Regardless of whether the member type is an array type or not
            // member can already have an XmlElementAttribute attribute.
            // If this is the case we can simply add the XML type name information to that.
            CodeAttributeDeclaration xmlElementAttribute = memberExtension.FindAttribute("System.Xml.Serialization.XmlElementAttribute");

            if (xmlElementAttribute != null)
            {
                // Create a new CodeAttributeDeclaration with required arguments
                xmlElementAttribute = new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
                                                                   new CodeAttributeArgumentExtended(
                                                                       "ElementName", new CodePrimitiveExpression(oldName), true));

                // Add the newly created attribute to CodeTypeMember.
                memberExtension.AddAttribute(xmlElementAttribute);
                // No need to proceed, so simply return.
                return;
            }

            // Let's first handle the non-array case.
            // And then handl the array case.
            if (!PascalCaseConverterHelper.IsArray(memberExtension))
            {
                // See if we can spot the XmlAttributeAttribute attribute.
                CodeAttributeDeclaration xmlAttribute = memberExtension.FindAttribute("System.Xml.Serialization.XmlAttributeAttribute");
                // If we could, then let's add the AttributeName argument to it.
                if (xmlAttribute != null)
                {
                    // Create a new CodeAttributeDeclaration with required arguments.
                    CodeAttributeDeclaration xmlAttributeAttribute =
                        new CodeAttributeDeclaration("System.Xml.Serialization.XmlAttributeAttribute",
                                                     new CodeAttributeArgumentExtended(
                                                         "AttributeName", new CodePrimitiveExpression(oldName), true));

                    // Add the newly created attribute to CodeTypeMember.
                    memberExtension.AddAttribute(xmlAttributeAttribute);
                }
                else
                {
                    // We arrive here if we could not spot the XmlAttributeAttribute attribute.
                    // Therefore we can add the XmlElementAttribute attribute.
                    // However, before we proceed we have to check whether any of the following attributes
                    // already exists.
                    if (memberExtension.FindAttribute("System.Xml.Serialization.XmlTextAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlIgnoreAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null)
                    {
                        // We cannot add XmlElementAttribute attribute here.
                        return;
                    }

                    // Create a new CodeAttributeDeclaration with required arguments
                    xmlElementAttribute = new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
                                                                       new CodeAttributeArgumentExtended(
                                                                           "ElementName", new CodePrimitiveExpression(oldName), true));

                    // Add the newly created attribute to CodeTypeMember.
                    memberExtension.AddAttribute(xmlElementAttribute);
                }
            }
            else
            {
                // We arrive here if we have an array type.
                // We can proceed to adding XmlArrayAttribue attribute if following attributes are
                // not present.
                if (memberExtension.FindAttribute("System.Xml.Serialization.XmlTextAttribute") != null ||
                    memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                    memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null)
                {
                    // We cannot add XmlElementAttribute attribute here.
                    return;
                }

                // Create a new CodeAttributeDeclaration for XmlArrayAttribute with required arguments.
                CodeAttributeDeclaration xmlArrayAttribute =
                    new CodeAttributeDeclaration("System.Xml.Serialization.XmlArrayAttribute",
                                                 new CodeAttributeArgumentExtended(
                                                     "ElementName", new CodePrimitiveExpression(oldName), true));

                // Add the newly created CodeAttributeDeclaration to the attributes collection of
                // CodeTypeMemeber.
                memberExtension.AddAttribute(xmlArrayAttribute);
            }
        }
Example #7
0
        protected override void OnEnumMemberChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Fix references found in DefaultValue attributes.
            foreach (CodeTypeExtension type in Code.DataContracts)
            {
                foreach (CodeTypeMemberExtension member in type.Fields)
                {
                    CodeAttributeDeclaration attribute = member.FindAttribute("System.ComponentModel.DefaultValueAttribute");
                    if (attribute == null)
                    {
                        continue;
                    }

                    CodeAttributeArgument        argument      = attribute.Arguments[0];
                    CodeFieldReferenceExpression argumentValue = argument.Value as CodeFieldReferenceExpression;
                    if (argumentValue == null)
                    {
                        continue;
                    }

                    string baseTypeName           = ((CodeTypeReferenceExpression)argumentValue.TargetObject).Type.BaseType;
                    string nameOfTypeInAttribute  = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                    if (argumentValue.FieldName == oldName && nameOfTypeInAttribute == nameOfTypeBeingChanged)
                    {
                        argumentValue.FieldName = newName;
                    }
                }

                // Fix references found in constructor where default values are set.
                // This is required for fixed references to enum values.
                // e.g. <xs:attribute ref="xlink:type" fixed="simple"/>
                foreach (CodeTypeMemberExtension ctorExtension in type.Constructors)
                {
                    // Get a reference to the actual constructor object.
                    CodeConstructor constructor = (CodeConstructor)ctorExtension.ExtendedObject;

                    // Do this for all statements we have in the constructor.
                    foreach (CodeStatement statement in constructor.Statements)
                    {
                        // Is this an assign statement?
                        CodeAssignStatement assignStatement = statement as CodeAssignStatement;
                        if (assignStatement != null)
                        {
                            // Do we have a field reference on the right side of the assignment statement?
                            CodeFieldReferenceExpression fieldRef = assignStatement.Right as CodeFieldReferenceExpression;
                            if (fieldRef != null)
                            {
                                // Does the referenced field belong to a type reference?
                                if (typeof(CodeTypeReferenceExpression) == fieldRef.TargetObject.GetType())
                                {
                                    string baseTypeName           = ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType;
                                    string nameOfTypeForField     = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                                    // Change the field name if it's changed.
                                    if (fieldRef.FieldName == oldName && nameOfTypeForField == nameOfTypeBeingChanged)
                                    {
                                        // Fix the field name first.
                                        fieldRef.FieldName = newName;

                                        // Also fix the name in the type reference.
                                        ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType = nameOfTypeForField;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Before adding the XmlEnumAttribute attribute to the CodeTypeMember
            // we have to make sure that the following attributes are not present.
            // If the 'XmlEnumAttribute' is already present the original value was not a valid name
            // and there should be no attempt to perform a rename.
            if (memberExtension.FindAttribute("System.Xml.Serialization.XmlAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlEnumAttribute") != null)
            {
                // We cannot proceed.
                return;
            }
            // Create a CodeAttributeDeclaration for XmlEnumAttribute attribute and
            // add it to the attributes collection.
            CodeAttributeDeclaration xmlEnum = new CodeAttributeDeclaration
                                                   ("System.Xml.Serialization.XmlEnumAttribute");

            xmlEnum.Arguments.Add(new CodeAttributeArgumentExtended("Name",
                                                                    new CodePrimitiveExpression(oldName), true));

            // Finally add it to the custom attributes collection.
            memberExtension.AddAttribute(xmlEnum);
        }
Example #8
0
        protected override void OnEnumMemberChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Fix references found in DefaultValue attributes.
            foreach (CodeTypeExtension type in Code.DataContracts)
            {
                foreach (CodeTypeMemberExtension member in type.Fields)
                {
                    CodeAttributeDeclaration attribute = member.FindAttribute("System.ComponentModel.DefaultValueAttribute");
                    if (attribute == null) continue;

                    CodeAttributeArgument argument = attribute.Arguments[0];
                    CodeFieldReferenceExpression argumentValue = argument.Value as CodeFieldReferenceExpression;
                    if (argumentValue == null) continue;

                    string baseTypeName = ((CodeTypeReferenceExpression)argumentValue.TargetObject).Type.BaseType;
                    string nameOfTypeInAttribute = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                    if (argumentValue.FieldName == oldName && nameOfTypeInAttribute == nameOfTypeBeingChanged)
                    {
                        argumentValue.FieldName = newName;
                    }
                }

                // Fix references found in constructor where default values are set.
                // This is required for fixed references to enum values.
                // e.g. <xs:attribute ref="xlink:type" fixed="simple"/>
                foreach (CodeTypeMemberExtension ctorExtension in type.Constructors)
                {
                    // Get a reference to the actual constructor object.
                    CodeConstructor constructor = (CodeConstructor)ctorExtension.ExtendedObject;

                    // Do this for all statements we have in the constructor.
                    foreach (CodeStatement statement in constructor.Statements)
                    {
                        // Is this an assign statement?
                        CodeAssignStatement assignStatement = statement as CodeAssignStatement;
                        if (assignStatement != null)
                        {
                            // Do we have a field reference on the right side of the assignment statement?
                            CodeFieldReferenceExpression fieldRef = assignStatement.Right as CodeFieldReferenceExpression;
                            if (fieldRef != null)
                            {
                                // Does the referenced field belong to a type reference?
                                if (typeof(CodeTypeReferenceExpression) == fieldRef.TargetObject.GetType())
                                {
                                    string baseTypeName = ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType;
                                    string nameOfTypeForField = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                                    // Change the field name if it's changed.
                                    if (fieldRef.FieldName == oldName && nameOfTypeForField == nameOfTypeBeingChanged)
                                    {
                                        // Fix the field name first.
                                        fieldRef.FieldName = newName;

                                        // Also fix the name in the type reference.
                                        ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType = nameOfTypeForField;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Before adding the XmlEnumAttribute attribute to the CodeTypeMember
            // we have to make sure that the following attributes are not present.
            // If the 'XmlEnumAttribute' is already present the original value was not a valid name
            // and there should be no attempt to perform a rename.
            if (memberExtension.FindAttribute("System.Xml.Serialization.XmlAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlEnumAttribute") != null)
            {
                // We cannot proceed.
                return;
            }
            // Create a CodeAttributeDeclaration for XmlEnumAttribute attribute and
            // add it to the attributes collection.
            CodeAttributeDeclaration xmlEnum = new CodeAttributeDeclaration
                ("System.Xml.Serialization.XmlEnumAttribute");
            xmlEnum.Arguments.Add(new CodeAttributeArgumentExtended("Name",
                new CodePrimitiveExpression(oldName), true));

            // Finally add it to the custom attributes collection.
            memberExtension.AddAttribute(xmlEnum);
        }
Example #9
0
        private void OnFieldOrPropertyNameChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Here we basically have two cases. Array and non-array.
            // If it's an non-array type, we have to decorate it with either
            // XmlAttributeAttribute attribute or XmlElementAttribute attribute.
            // If it's an array type we have to decorate it with XmlArrayAttribute
            // attribute.

            // There is one quickie we can try before anything nevertheless.
            // Regardless of whether the member type is an array type or not
            // member can already have an XmlElementAttribute attribute.
            // If this is the case we can simply add the XML type name information to that.
            CodeAttributeDeclaration xmlElementAttribute = memberExtension.FindAttribute("System.Xml.Serialization.XmlElementAttribute");
            if (xmlElementAttribute != null)
            {
                // Create a new CodeAttributeDeclaration with required arguments
                xmlElementAttribute = new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
                    new CodeAttributeArgumentExtended(
                    "ElementName", new CodePrimitiveExpression(oldName), true));

                // Add the newly created attribute to CodeTypeMember.
                memberExtension.AddAttribute(xmlElementAttribute);
                // No need to proceed, so simply return.
                return;
            }

            // Let's first handle the non-array case.
            // And then handl the array case.
            if (!PascalCaseConverterHelper.IsArray(memberExtension))
            {
                // See if we can spot the XmlAttributeAttribute attribute.
                CodeAttributeDeclaration xmlAttribute = memberExtension.FindAttribute("System.Xml.Serialization.XmlAttributeAttribute");
                // If we could, then let's add the AttributeName argument to it.
                if (xmlAttribute != null)
                {
                    // Create a new CodeAttributeDeclaration with required arguments.
                    CodeAttributeDeclaration xmlAttributeAttribute =
                        new CodeAttributeDeclaration("System.Xml.Serialization.XmlAttributeAttribute",
                        new CodeAttributeArgumentExtended(
                        "AttributeName", new CodePrimitiveExpression(oldName), true));

                    // Add the newly created attribute to CodeTypeMember.
                    memberExtension.AddAttribute(xmlAttributeAttribute);
                }
                else
                {
                    // We arrive here if we could not spot the XmlAttributeAttribute attribute.
                    // Therefore we can add the XmlElementAttribute attribute.
                    // However, before we proceed we have to check whether any of the following attributes
                    // already exists.
                    if (memberExtension.FindAttribute("System.Xml.Serialization.XmlTextAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlIgnoreAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null)
                    {
                        // We cannot add XmlElementAttribute attribute here.
                        return;
                    }

                    // Create a new CodeAttributeDeclaration with required arguments
                    xmlElementAttribute = new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
                        new CodeAttributeArgumentExtended(
                        "ElementName", new CodePrimitiveExpression(oldName), true));

                    // Add the newly created attribute to CodeTypeMember.
                    memberExtension.AddAttribute(xmlElementAttribute);
                }
            }
            else
            {
                // We arrive here if we have an array type.
                // We can proceed to adding XmlArrayAttribue attribute if following attributes are
                // not present.
                if (memberExtension.FindAttribute("System.Xml.Serialization.XmlTextAttribute") != null ||
                    memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                    memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null)
                {
                    // We cannot add XmlElementAttribute attribute here.
                    return;
                }

                // Create a new CodeAttributeDeclaration for XmlArrayAttribute with required arguments.
                CodeAttributeDeclaration xmlArrayAttribute =
                    new CodeAttributeDeclaration("System.Xml.Serialization.XmlArrayAttribute",
                    new CodeAttributeArgumentExtended(
                    "ElementName", new CodePrimitiveExpression(oldName), true));

                // Add the newly created CodeAttributeDeclaration to the attributes collection of
                // CodeTypeMemeber.
                memberExtension.AddAttribute(xmlArrayAttribute);
            }
        }
		private static void HandleShouldSerialize(CodeTypeMemberExtension fieldPropertyMember)
		{
			CodeTypeDeclaration type = fieldPropertyMember.Parent.ExtendedObject as CodeTypeDeclaration;
			if (type == null) return;

			CodeAttributeDeclaration arrayAttribute = fieldPropertyMember.FindAttribute("System.Xml.Serialization.XmlArrayItemAttribute");
			if (arrayAttribute == null) return;

			CodeAttributeArgument isNullableArgument = arrayAttribute.FindArgument("IsNullable");
			if (isNullableArgument == null) return;

			bool isNullable = (bool)((CodePrimitiveExpression)isNullableArgument.Value).Value;
			if (isNullable) return;

			string name = fieldPropertyMember.ExtendedObject.Name;

			CodeMemberMethod shouldSerializeMethod = new CodeMemberMethod
			{
				Attributes = MemberAttributes.Public,
				Name = "ShouldSerialize" + name,
				ReturnType = new CodeTypeReference(typeof(bool))
			};


			CodeThisReferenceExpression thisReference = new CodeThisReferenceExpression();
			CodeFieldReferenceExpression collectionField = new CodeFieldReferenceExpression(thisReference, name);
			CodeBinaryOperatorExpression notNullExpression = new CodeBinaryOperatorExpression
			{
				Left = collectionField, 
				Operator = CodeBinaryOperatorType.IdentityInequality, 
				Right = new CodePrimitiveExpression(null)
			};

			CodeBinaryOperatorExpression greaterThanZeroExpression = new CodeBinaryOperatorExpression
			{
				Left = new CodePropertyReferenceExpression(collectionField, "Count"),
				Operator = CodeBinaryOperatorType.GreaterThan,
				Right = new CodePrimitiveExpression(0)
			};

			CodeBinaryOperatorExpression andExpression = new CodeBinaryOperatorExpression
			{
				Left = notNullExpression,
				Operator = CodeBinaryOperatorType.BooleanAnd,
				Right = greaterThanZeroExpression
			};

			CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(andExpression);
			shouldSerializeMethod.Statements.Add(returnStatement);

			type.Members.Add(shouldSerializeMethod);
		}
 /// <summary>
 /// This method checks whether a given CodeTypeMember contains any attributes that will
 /// prevent from converting its type from an array to a collection.
 /// </summary>
 private bool HasInvalidAttributes(CodeTypeMemberExtension memberExtension)
 {
     if (memberExtension.FindAttribute("System.Xml.Serialization.XmlChoiceIdentifierAttribute") != null)
     {
         return true;
     }
     else if (memberExtension.FindAttribute("System.Xml.Serialization.XmlIgnoreAttribute") != null)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Example #12
0
        private static void HandleShouldSerialize(CodeTypeMemberExtension fieldPropertyMember)
        {
            CodeTypeDeclaration type = fieldPropertyMember.Parent.ExtendedObject as CodeTypeDeclaration;

            if (type == null)
            {
                return;
            }

            CodeAttributeDeclaration arrayAttribute = fieldPropertyMember.FindAttribute("System.Xml.Serialization.XmlArrayItemAttribute");

            if (arrayAttribute == null)
            {
                return;
            }

            CodeAttributeArgument isNullableArgument = arrayAttribute.FindArgument("IsNullable");

            if (isNullableArgument == null)
            {
                return;
            }

            bool isNullable = (bool)((CodePrimitiveExpression)isNullableArgument.Value).Value;

            if (isNullable)
            {
                return;
            }

            string name = fieldPropertyMember.ExtendedObject.Name;

            CodeMemberMethod shouldSerializeMethod = new CodeMemberMethod
            {
                Attributes = MemberAttributes.Public,
                Name       = "ShouldSerialize" + name,
                ReturnType = new CodeTypeReference(typeof(bool))
            };


            CodeThisReferenceExpression  thisReference     = new CodeThisReferenceExpression();
            CodeFieldReferenceExpression collectionField   = new CodeFieldReferenceExpression(thisReference, name);
            CodeBinaryOperatorExpression notNullExpression = new CodeBinaryOperatorExpression
            {
                Left     = collectionField,
                Operator = CodeBinaryOperatorType.IdentityInequality,
                Right    = new CodePrimitiveExpression(null)
            };

            CodeBinaryOperatorExpression greaterThanZeroExpression = new CodeBinaryOperatorExpression
            {
                Left     = new CodePropertyReferenceExpression(collectionField, "Count"),
                Operator = CodeBinaryOperatorType.GreaterThan,
                Right    = new CodePrimitiveExpression(0)
            };

            CodeBinaryOperatorExpression andExpression = new CodeBinaryOperatorExpression
            {
                Left     = notNullExpression,
                Operator = CodeBinaryOperatorType.BooleanAnd,
                Right    = greaterThanZeroExpression
            };

            CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(andExpression);

            shouldSerializeMethod.Statements.Add(returnStatement);

            type.Members.Add(shouldSerializeMethod);
        }