private static void RemoveSpecifiedProperties(CodeTypeDeclaration codeType, CodeNamespace codeNamespace, bool promoteToNullable)
    {
        var toRemove = new List <CodeTypeMember>();

        foreach (var property in codeType.Members.OfType <CodeMemberProperty>())
        {
            CodeMemberField    backingField;
            CodeMemberProperty specifiedProperty;
            if (!property.TryGetBackingFieldAndSpecifiedProperty(codeType, out backingField, out specifiedProperty))
            {
                continue;
            }
            var specifiedField = specifiedProperty.GetBackingField(codeType);
            if (specifiedField == null)
            {
                continue;
            }
            toRemove.Add(specifiedProperty);
            toRemove.Add(specifiedField);
            if (promoteToNullable)
            {
                // Do not do this for attributes
                if (property.CustomAttributes.Cast <CodeAttributeDeclaration>().Any(a => a.AttributeType.BaseType == typeof(System.Xml.Serialization.XmlAttributeAttribute).FullName))
                {
                    continue;
                }
                var typeRef = property.Type;
                if (typeRef.ArrayRank > 0)
                {
                    // An array - not a reference type.
                    continue;
                }
                // OK, two possibilities here:
                // 1) The property might reference some system type such as DateTime or decimal
                // 2) The property might reference some type being defined such as an enum or struct.
                var type = Type.GetType(typeRef.BaseType);
                if (type != null)
                {
                    if (!type.IsClass)
                    {
                        if (type == typeof(Nullable <>))
                        {
                            // Already nullable
                            continue;
                        }
                        else if (!type.IsGenericTypeDefinition && (type.IsValueType || type.IsEnum) && Nullable.GetUnderlyingType(type) == null)
                        {
                            var nullableType = typeof(Nullable <>).MakeGenericType(type);
                            var newRefType   = new CodeTypeReference(nullableType);
                            property.Type     = newRefType;
                            backingField.Type = newRefType;
                        }
                    }
                }
                else
                {
                    var generatedType = codeNamespace.FindCodeType(typeRef);
                    if (generatedType != null)
                    {
                        if (generatedType.IsStruct || generatedType.IsEnum)
                        {
                            var newRefType = new CodeTypeReference(typeof(Nullable <>).FullName, typeRef);
                            property.Type     = newRefType;
                            backingField.Type = newRefType;
                        }
                    }
                }
            }
        }
        foreach (var member in toRemove)
        {
            codeType.Members.Remove(member);
        }
    }