public static CodeTypeReference GetEntityClassTypeReference(WXMLCodeDomGeneratorSettings settings,
                                                                    EntityDefinition entityDesc, bool appendNamespace)
        {
            string className = new WXMLCodeDomGeneratorNameHelper(settings).GetEntityClassName(entityDesc, appendNamespace);

            return(new CodeTypeReference(className));
        }
        public static CodeExpression GetFieldNameReferenceExpression(WXMLCodeDomGeneratorSettings settings, PropertyDefinition propertyDesc, bool addNamespace)
        {
            string className = new WXMLCodeDomGeneratorNameHelper(settings)
                               .GetEntityClassName(propertyDesc.Entity, addNamespace) + ".Properties";

            return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(className),
                                                    propertyDesc.PropertyAlias));
        }
 public static CodeExpression GetEntityNameReferenceExpression(WXMLCodeDomGeneratorSettings settings, EntityDefinition entityDescription, bool addNamespace)
 {
     if (entityDescription.BaseEntity != null && entityDescription.BaseEntity.FamilyName == entityDescription.FamilyName)
     {
         return(GetEntityNameReferenceExpression(settings, entityDescription.BaseEntity, true));
     }
     else
     {
         string className = new WXMLCodeDomGeneratorNameHelper(settings).GetEntityClassName(entityDescription, addNamespace) + ".Descriptor";
         return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(className), "EntityName"));
     }
 }
        public void DefaultUpdateSetValueMethod(PropertyDefinition propertyDesc, CodeMemberMethod setvalueMethod)
        {
            //Type fieldRealType;
            //fieldRealType = Type.GetType(field.Type.BaseType, false);

            var setValueStatement = new CodeConditionStatement(
                new CodeMethodInvokeExpression(
                    WXMLCodeDomGeneratorHelper.GetFieldNameReferenceExpression(propertyDesc),
                    "Equals",
                    new CodeVariableReferenceExpression("fieldName"))
                );

            var fieldName = new WXMLCodeDomGeneratorNameHelper(Settings).GetPrivateMemberName(propertyDesc.Name);

            //setValueStatement.TrueStatements.Add(
            //    new CodeVariableDeclarationStatement(typeof(IConvertible), "vConv",
            //        new Codety)
            //    );

            //old: simple cast
            //setValueStatement.TrueStatements.Add(new CodeAssignStatement(
            //                         new CodeFieldReferenceExpression(
            //                             new CodeThisReferenceExpression(), field.Name),
            //                         new CodeCastExpression(field.Type,
            //                                                new CodeArgumentReferenceExpression(
            //                                                    "value"))));

            // new: solves problem of direct casts with Nullable<>
            if (propertyDesc.PropertyType.IsNullableType && propertyDesc.PropertyType.IsClrType && propertyDesc.PropertyType.ClrType.GetGenericArguments()[0].IsValueType && !propertyDesc.PropertyType.ClrType.GetGenericArguments()[0].Equals(typeof(Guid)) && !propertyDesc.PropertyType.ClrType.GetGenericArguments()[0].Equals(typeof(TimeSpan)))
            {
                setValueStatement.TrueStatements.Add(
                    //new CodeVariableDeclarationStatement(typeof(IConvertible), "iconvVal",
                    //                                     CodePatternAsExpression(new CodeTypeReference(typeof(IConvertible)),
                    //                                                             new CodeArgumentReferenceExpression("value")))
                    Emit.declare("iconvVal", () => LinqToCodedom.Generator.CodeDom.VarRef("value") as IConvertible)
                    );
                setValueStatement.TrueStatements.Add(
                    new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("iconvVal"),
                                                         CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)),
                        new CodeStatement[]
                {
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(), fieldName),
                        new CodeCastExpression(propertyDesc.PropertyType.ToCodeType(Settings),
                                               new CodeArgumentReferenceExpression(
                                                   "value")))
                },
                        new CodeStatement[]
                {
                    //System.Threading.Thread.CurrentThread.CurrentCulture
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(), fieldName),
                        new CodeMethodInvokeExpression(
                            new CodeVariableReferenceExpression("iconvVal"),
                            "To" + propertyDesc.PropertyType.ClrType.GetGenericArguments()[0].Name,
                            new CodePropertyReferenceExpression(
                                new CodePropertyReferenceExpression(
                                    new CodeTypeReferenceExpression(typeof(System.Threading.Thread)),
                                    "CurrentThread"
                                    ),
                                "CurrentCulture"
                                )
                            )
                        )
                }
                        )
                    );
            }
            else if (propertyDesc.PropertyType.IsEnum)
            {
                setValueStatement.TrueStatements.Add(
                    Emit.@ifelse((object value) => LinqToCodedom.Generator.CodeDom.Is(value, null),
                                 /* true statements */ new[] { Emit.assignField(fieldName, (object value) => LinqToCodedom.Generator.CodeDom.@default(propertyDesc.PropertyType.ToCodeType(Settings))) },
                                 /* false statements */ Emit.assignField(fieldName, (object value) => LinqToCodedom.Generator.CodeDom.cast(propertyDesc.PropertyType.ToCodeType(Settings),
                                                                                                                                           LinqToCodedom.Generator.CodeDom.Call <object>(LinqToCodedom.Generator.CodeDom.MethodRef(typeof(Enum), "ToObject"))(new CodeTypeOfExpression(propertyDesc.PropertyType.ToCodeType(Settings)), value)
                                                                                                                                           ))
                                 )
                    );
            }
            else if (propertyDesc.PropertyType.IsValueType && (!propertyDesc.PropertyType.IsNullableType || !(propertyDesc.PropertyType.IsClrType && (propertyDesc.PropertyType.ClrType.GetGenericArguments()[0].Equals(typeof(Guid)) || propertyDesc.PropertyType.ClrType.GetGenericArguments()[0].Equals(typeof(TimeSpan))))))
            {
                //old: simple cast
                //setValueStatement.TrueStatements.Add(new CodeAssignStatement(
                //                         new CodeFieldReferenceExpression(
                //                             new CodeThisReferenceExpression(), fieldName),
                //                         new CodeCastExpression(propertyDesc.PropertyType.ToCodeType(Settings),
                //                             new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(new CodeTypeReference(typeof(Convert))), "ChangeType",
                //                                                new CodeArgumentReferenceExpression("value"), new CodeTypeOfExpression(propertyDesc.PropertyType.ToCodeType(Settings))))));
                setValueStatement.TrueStatements.Add(
                    Emit.@ifelse((object value) => LinqToCodedom.Generator.CodeDom.Is(value, null),
                                 /* true statements */ new [] { Emit.assignField(fieldName, (object value) => LinqToCodedom.Generator.CodeDom.@default(propertyDesc.PropertyType.ToCodeType(Settings))) },
                                 /* false statements */ Emit.assignField(fieldName, (object value) => LinqToCodedom.Generator.CodeDom.cast(propertyDesc.PropertyType.ToCodeType(Settings),
                                                                                                                                           LinqToCodedom.Generator.CodeDom.Call <object>("Convert.ChangeType")(value, new CodeTypeOfExpression(propertyDesc.PropertyType.ToCodeType(Settings)))
                                                                                                                                           ))
                                 )
                    );
            }
            else
            {
                setValueStatement.TrueStatements.Add(new CodeAssignStatement(
                                                         new CodeFieldReferenceExpression(
                                                             new CodeThisReferenceExpression(), fieldName),
                                                         new CodeCastExpression(propertyDesc.PropertyType.ToCodeType(Settings), new CodeArgumentReferenceExpression("value"))));
            }
            setValueStatement.TrueStatements.Add(new CodeMethodReturnStatement());
            setvalueMethod.Statements.Add(setValueStatement);
        }
        public void EnumPervUpdateSetValueMethod(PropertyDefinition propertyDesc, CodeMemberMethod setvalueMethod)
        {
            var fieldName = new WXMLCodeDomGeneratorNameHelper(Settings).GetPrivateMemberName(propertyDesc.Name);

            if (propertyDesc.PropertyType.IsEnum)
            {
                var setValueStatement = new CodeConditionStatement(
                    new CodeMethodInvokeExpression(
                        WXMLCodeDomGeneratorHelper.GetFieldNameReferenceExpression(propertyDesc),
                        "Equals",
                        new CodeVariableReferenceExpression("fieldName"))

                    );
                if (propertyDesc.PropertyType.IsNullableType)
                {
                    setValueStatement.TrueStatements.Add(
                        new CodeConditionStatement(
                            new CodeBinaryOperatorExpression(
                                new CodeArgumentReferenceExpression("value"),
                                CodeBinaryOperatorType.IdentityEquality,
                                new CodePrimitiveExpression(null)
                                ),
                            new CodeStatement[]
                    {
                        new CodeAssignStatement(
                            new CodeFieldReferenceExpression(
                                new CodeThisReferenceExpression(), fieldName),
                            new CodeDefaultValueExpression(propertyDesc.PropertyType.ToCodeType(Settings)))
                    },
                            new CodeStatement[]
                    {
                        new CodeVariableDeclarationStatement(
                            new CodeTypeReference(typeof(Type)),
                            "t",
                            new CodeArrayIndexerExpression(
                                new CodeMethodInvokeExpression(
                                    new CodeTypeOfExpression(propertyDesc.PropertyType.ToCodeType(Settings)),
                                    "GetGenericArguments"
                                    ),
                                new CodePrimitiveExpression(0)
                                )
                            ),
                        new CodeAssignStatement(
                            new CodeFieldReferenceExpression(
                                new CodeThisReferenceExpression(), fieldName),
                            new CodeCastExpression(
                                propertyDesc.PropertyType.ToCodeType(Settings),
                                new CodeMethodInvokeExpression(
                                    new CodeTypeReferenceExpression(typeof(Enum)),
                                    "ToObject",
                                    // typeof(Nullable<int>).GetGenericArguments()[0]
                                    new CodeVariableReferenceExpression("t"),
                                    new CodeArgumentReferenceExpression(
                                        "value")
                                    )))
                    }

                            )
                        );
                }
                else
                {
                    setValueStatement.TrueStatements.Add(new CodeAssignStatement(
                                                             new CodeFieldReferenceExpression(
                                                                 new CodeThisReferenceExpression(), fieldName),
                                                             new CodeCastExpression(
                                                                 propertyDesc.PropertyType.ToCodeType(Settings),
                                                                 new CodeMethodInvokeExpression(
                                                                     new CodeTypeReferenceExpression(typeof(Enum)),
                                                                     "ToObject",
                                                                     // typeof(Nullable<int>).GetGenericArguments()[0]
                                                                     new CodeTypeOfExpression(propertyDesc.PropertyType.ToCodeType(Settings)),
                                                                     new CodeArgumentReferenceExpression(
                                                                         "value")
                                                                     ))));
                }
                setValueStatement.TrueStatements.Add(new CodeMethodReturnStatement());
                setvalueMethod.Statements.Add(setValueStatement);
            }
            else
            {
                DefaultUpdateSetValueMethod(propertyDesc, setvalueMethod);
            }
        }