public static void AddVariableDeclaration(Type type, string name, CodeStatementCollection statements)
        {
            for (int i = 0; i < statements.Count; i++)
            {
                CodeVariableDeclarationStatement vd = statements[i] as CodeVariableDeclarationStatement;
                if (vd != null)
                {
                    if (vd.Name == name)
                    {
                        return;
                    }
                }
            }
            CodeVariableDeclarationStatement p;

            if (type.IsValueType)
            {
                p = new CodeVariableDeclarationStatement(type, name);
            }
            else
            {
                p = new CodeVariableDeclarationStatement(type, name, ObjectCreationCodeGen.GetDefaultValueExpression(type));
            }
            statements.Insert(0, p);
        }
        public static CodeExpression ConvertToBool(Type t, CodeExpression ce)
        {
            if (t.Equals(typeof(bool)))
            {
                return(ce);
            }
            if (t.Equals(typeof(string)))
            {
                CodeBinaryOperatorExpression ret = new CodeBinaryOperatorExpression();
                ret.Operator = CodeBinaryOperatorType.BooleanOr;
                CodeBinaryOperatorExpression b = new CodeBinaryOperatorExpression();
                b.Operator = CodeBinaryOperatorType.IdentityEquality;
                b.Right    = new CodePrimitiveExpression(0);
                CodeMethodInvokeExpression mi = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(string)), "Compare"),
                                                                               ce, new CodePrimitiveExpression("true"), new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(StringComparison)), "OrdinalIgnoreCase"));
                b.Left   = mi;
                ret.Left = b;
                //
                b          = new CodeBinaryOperatorExpression();
                b.Operator = CodeBinaryOperatorType.IdentityEquality;
                b.Right    = new CodePrimitiveExpression(0);
                mi         = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(string)), "Compare"),
                                                            ce, new CodePrimitiveExpression("on"), new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(StringComparison)), "OrdinalIgnoreCase"));
                b.Left    = mi;
                ret.Right = b;
                return(ret);
            }
            if (typeof(object).Equals(t) || typeof(byte).Equals(t) ||
                typeof(char).Equals(t) || typeof(DateTime).Equals(t) ||
                typeof(decimal).Equals(t) || typeof(double).Equals(t) ||
                typeof(float).Equals(t) || typeof(int).Equals(t) ||
                typeof(long).Equals(t) || typeof(sbyte).Equals(t) ||
                typeof(short).Equals(t) || typeof(uint).Equals(t) ||
                typeof(ulong).Equals(t) || typeof(ushort).Equals(t)
                )
            {
                CodeMethodInvokeExpression mie = new CodeMethodInvokeExpression();
                mie.Method = new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToBoolean");
                mie.Parameters.Add(ce);
                if (IsPrimaryExp(ce))
                {
                    return(mie);
                }
                CodeBinaryOperatorExpression bo = new CodeBinaryOperatorExpression();
                bo.Operator = CodeBinaryOperatorType.BooleanAnd;
                bo.Left     = new CodeBinaryOperatorExpression(ce, CodeBinaryOperatorType.IdentityInequality, new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(DBNull)), "Value"));
                bo.Right    = mie;
                return(bo);
            }
            CodeExpression def = ObjectCreationCodeGen.GetDefaultValueExpression(t);

            return(new CodeBinaryOperatorExpression(ce, CodeBinaryOperatorType.IdentityInequality, def));
        }
        public static CodeVariableDeclarationStatement CreateVariableDeclaration(CodeTypeReference type, string name, Type t, object init)
        {
            CodeVariableDeclarationStatement p;

            if (init == null)
            {
                p = new CodeVariableDeclarationStatement(type, name, ObjectCreationCodeGen.GetDefaultValueExpression(t));
            }
            else
            {
                p = new CodeVariableDeclarationStatement(type, name, ObjectCreationCodeGen.ObjectCreationCode(init));
            }
            return(p);
        }