private CodeMemberProperty CreateArrayPropertyWithoutChoice(EnhancedProperty enhancedProperty, string codeTypeMemberName)
        {
            var propertyName = "Object" + codeTypeMemberName;
            var property     = CreateProperty(enhancedProperty, true);

            //return ObjectItems.GetItems<type>(name);
            property.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(propertyName), "GetItems", new CodeTypeReference(enhancedProperty.Type)),
                        new CodePrimitiveExpression(enhancedProperty.Name)
                        )
                    )
                );

            //ObjectItems.SetItems(name, value.ToArray());
            property.SetStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(propertyName), "SetItems"),
                    new CodePrimitiveExpression(enhancedProperty.Name),
                    new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("value"), "ToArray")
                    )
                );

            return(property);
        }
        private CodeMemberProperty CreateProperty(EnhancedProperty enhancedProperty, bool array)
        {
            var type = enhancedProperty.Type;

            if (array)
            {
                type = "IEnumerable<" + type + ">";
            }

            if (enhancedProperty.IsNullable && !array)
            {
                type = "Nullable<" + type + ">";
            }

            var codeTypeReference = new CodeTypeReference(type);
            var property          = new CodeMemberProperty()
            {
                Type       = codeTypeReference,
                Name       = enhancedProperty.Name,
                Attributes = MemberAttributes.Public | MemberAttributes.Final
            };

            property.CustomAttributes.Add(ignoreAttribute);

            return(property);
        }
        private CodeMemberProperty CreateAppropriateProperty(string withEnumChoice, EnhancedProperty enhancedProperty, bool isArray, string codeTypeMemberName, XmlSchemaComplexType xmlSchemaComplexType, IList <string> nameOrder)
        {
            CodeMemberProperty property = null;

            if (string.IsNullOrEmpty(withEnumChoice))
            {
                if (isArray)
                {
                    property = CreateArrayPropertyWithoutChoice(enhancedProperty, codeTypeMemberName);
                }
                else
                {
                    property = CreateSinglePropertyWithoutChoice(enhancedProperty, codeTypeMemberName, xmlSchemaComplexType);
                }
            }
            else
            {
                if (isArray)
                {
                    property = CreateArrayPropertyWithChoice(enhancedProperty, withEnumChoice, codeTypeMemberName);
                }
                else
                {
                    property = CreateSinglePropertyWithChoice(enhancedProperty, withEnumChoice, codeTypeMemberName, xmlSchemaComplexType);
                }
            }

            return(property);
        }
        private EnhancedProperty GetEnhancedPropertyFromAttributeAndUpdateAttribute(CodeAttributeDeclaration attribute)
        {
            var enhancedProperty = new EnhancedProperty();

            for (var i = 0; i < attribute.Arguments.Count; i++)
            {
                if (i == 0)
                {
                    CodePrimitiveExpression arg1 = attribute.Arguments[i].Value as CodePrimitiveExpression;
                    enhancedProperty.Name = arg1.Value as string;
                }
                else if (i == 1)
                {
                    CodeTypeOfExpression arg2 = attribute.Arguments[i].Value as CodeTypeOfExpression;
                    enhancedProperty.Type = arg2.Type.BaseType + string.Concat(Enumerable.Repeat("[]", arg2.Type.ArrayRank));
                }

                /* // NOT WORKING with XmlSerializer.
                 * // If we remove the DataType attribute we get a "cannot create temporary class"
                 * // If we keep as int32, it says it doesn't match the type string.
                 * else if (i == 2)
                 * {
                 * CodePrimitiveExpression arg3 = attribute.Arguments[i].Value as CodePrimitiveExpression;
                 * if ((string)arg3.Value == "integer")
                 * {
                 *  enhancedProperty.Type = "System.Int32";
                 *
                 *  // Change the arg2 to be int and remove arg3
                 *  //(attribute.Arguments[1].Value as CodeTypeOfExpression).Type = new CodeTypeReference("System.Int32");
                 *  //attribute.Arguments.RemoveAt(2);
                 *  //break;
                 * }
                 * }
                 * */
            }

            var realType = Type.GetType(enhancedProperty.Type);

            if (realType == null) // We found a type that is part of our special types
            {
                var specialType = codeNamespaceTypes.FirstOrDefault(m => m.Name == enhancedProperty.Type);
                if (specialType != null)
                {
                    if (specialType.IsEnum)
                    {
                        enhancedProperty.IsNullable = true;
                    }
                }
            }
            else if (realType.IsValueType)
            {
                enhancedProperty.IsNullable = true;
            }

            return(enhancedProperty);
        }
        private CodeMemberProperty CreateSinglePropertyWithoutChoice(EnhancedProperty enhancedProperty, string codeTypeMemberName, XmlSchemaComplexType xmlSchemaComplexType)
        {
            var propertyName = "Object" + codeTypeMemberName;
            var property     = CreateProperty(enhancedProperty, false);

            //return ObjectItems.GetItem<type>(name);
            property.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(propertyName), "GetItem", new CodeTypeReference(enhancedProperty.Type)),
                        new CodePrimitiveExpression(enhancedProperty.Name)
                        )
                    )
                );

            var saveReference = new CodeVariableReferenceExpression("value");

            if (enhancedProperty.Type == "System.String")
            {
                var schemaElement = GetSchemaElement(xmlSchemaComplexType, enhancedProperty.Name);
                if (schemaElement != null)
                {
                    var restriction = GetStringRestriction(schemaElement);
                    if (restriction != null)
                    {
                        var intValue = int.Parse(restriction.Value);
                        AddStringLengthAttribute(property, intValue);
                        saveReference = AddSubstringSetStatement(property.SetStatements, intValue);
                    }
                }
            }

            //ObjectItems.SetItem(name, value);
            property.SetStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(propertyName), "SetItem"),
                    new CodePrimitiveExpression(enhancedProperty.Name),
                    saveReference
                    )
                );

            return(property);
        }