コード例 #1
0
        internal void AddRowColumnProperties(CodeTypeDeclaration rowClass)
        {
            bool   flag = false;
            string generatorRowClassName = this.codeGenerator.TableHandler.Tables[this.table.TableName].GeneratorRowClassName;
            string generatorTableVarName = this.codeGenerator.TableHandler.Tables[this.table.TableName].GeneratorTableVarName;

            foreach (DesignColumn column in this.columns)
            {
                CodeExpression     expression;
                CodeExpression     expression2;
                DataColumn         dataColumn = column.DataColumn;
                Type               dataType   = dataColumn.DataType;
                string             generatorColumnPropNameInRow   = column.GeneratorColumnPropNameInRow;
                string             generatorColumnPropNameInTable = column.GeneratorColumnPropNameInTable;
                GenericNameHandler handler  = new GenericNameHandler(new string[] { generatorColumnPropNameInRow }, this.codeGenerator.CodeProvider);
                CodeMemberProperty property = CodeGenHelper.PropertyDecl(CodeGenHelper.Type(dataType), generatorColumnPropNameInRow, MemberAttributes.Public | MemberAttributes.Final);
                CodeStatement      tryStmnt = CodeGenHelper.Return(CodeGenHelper.Cast(CodeGenHelper.GlobalType(dataType), CodeGenHelper.Indexer(CodeGenHelper.This(), CodeGenHelper.Property(CodeGenHelper.Field(CodeGenHelper.This(), generatorTableVarName), generatorColumnPropNameInTable))));
                if (!dataColumn.AllowDBNull)
                {
                    goto Label_0440;
                }
                string str5 = (string)dataColumn.ExtendedProperties["nullValue"];
                switch (str5)
                {
                case null:
                case "_throw":
                    tryStmnt = CodeGenHelper.Try(tryStmnt, CodeGenHelper.Catch(CodeGenHelper.GlobalType(typeof(InvalidCastException)), handler.AddNameToList("e"), CodeGenHelper.Throw(CodeGenHelper.GlobalType(typeof(StrongTypingException)), System.Design.SR.GetString("CG_ColumnIsDBNull", new object[] { dataColumn.ColumnName, this.table.TableName }), handler.GetNameFromList("e"))));
                    goto Label_0440;

                default:
                {
                    expression = null;
                    switch (str5)
                    {
                    case "_null":
                        if (dataColumn.DataType.IsSubclassOf(typeof(System.ValueType)))
                        {
                            this.codeGenerator.ProblemList.Add(new DSGeneratorProblem(System.Design.SR.GetString("CG_TypeCantBeNull", new object[] { dataColumn.ColumnName, dataColumn.DataType.Name }), ProblemSeverity.NonFatalError, column));
                            continue;
                        }
                        expression2 = CodeGenHelper.Primitive(null);
                        goto Label_03AE;

                    case "_empty":
                        if (dataColumn.DataType == typeof(string))
                        {
                            expression2 = CodeGenHelper.Property(CodeGenHelper.TypeExpr(CodeGenHelper.GlobalType(dataColumn.DataType)), "Empty");
                        }
                        else
                        {
                            expression2 = CodeGenHelper.Field(CodeGenHelper.TypeExpr(CodeGenHelper.Type(generatorRowClassName)), generatorColumnPropNameInRow + "_nullValue");
                            ConstructorInfo constructor = dataColumn.DataType.GetConstructor(new Type[] { typeof(string) });
                            if (constructor == null)
                            {
                                this.codeGenerator.ProblemList.Add(new DSGeneratorProblem(System.Design.SR.GetString("CG_NoCtor0", new object[] { dataColumn.ColumnName, dataColumn.DataType.Name }), ProblemSeverity.NonFatalError, column));
                                continue;
                            }
                            constructor.Invoke(new object[0]);
                            expression = CodeGenHelper.New(CodeGenHelper.Type(dataColumn.DataType), new CodeExpression[0]);
                        }
                        goto Label_03AE;
                    }
                    if (!flag)
                    {
                        this.table.NewRow();
                        flag = true;
                    }
                    object             valueObj = this.codeGenerator.RowHandler.RowGenerator.ConvertXmlToObject.Invoke(dataColumn, new object[] { str5 });
                    DSGeneratorProblem problem  = CodeGenHelper.GenerateValueExprAndFieldInit(column, valueObj, str5, generatorRowClassName, generatorColumnPropNameInRow + "_nullValue", out expression2, out expression);
                    if (problem != null)
                    {
                        this.codeGenerator.ProblemList.Add(problem);
                        continue;
                    }
                    break;
                }
                }
                Label_03AE :;
                tryStmnt = CodeGenHelper.If(CodeGenHelper.MethodCall(CodeGenHelper.This(), "Is" + generatorColumnPropNameInRow + "Null"), new CodeStatement[] { CodeGenHelper.Return(expression2) }, new CodeStatement[] { tryStmnt });
                if (expression != null)
                {
                    CodeMemberField field = CodeGenHelper.FieldDecl(CodeGenHelper.Type(dataColumn.DataType.FullName), generatorColumnPropNameInRow + "_nullValue");
                    field.Attributes     = MemberAttributes.Private | MemberAttributes.Static;
                    field.InitExpression = expression;
                    rowClass.Members.Add(field);
                }
Label_0440:
                property.GetStatements.Add(tryStmnt);
                property.SetStatements.Add(CodeGenHelper.Assign(CodeGenHelper.Indexer(CodeGenHelper.This(), CodeGenHelper.Property(CodeGenHelper.Field(CodeGenHelper.This(), generatorTableVarName), generatorColumnPropNameInTable)), CodeGenHelper.Value()));
                rowClass.Members.Add(property);
                if (dataColumn.AllowDBNull)
                {
                    string           name   = MemberNameValidator.GenerateIdName("Is" + generatorColumnPropNameInRow + "Null", this.codeGenerator.CodeProvider, false);
                    CodeMemberMethod method = CodeGenHelper.MethodDecl(CodeGenHelper.GlobalType(typeof(bool)), name, MemberAttributes.Public | MemberAttributes.Final);
                    method.Statements.Add(CodeGenHelper.Return(CodeGenHelper.MethodCall(CodeGenHelper.This(), "IsNull", CodeGenHelper.Property(CodeGenHelper.Field(CodeGenHelper.This(), generatorTableVarName), generatorColumnPropNameInTable))));
                    rowClass.Members.Add(method);
                    name = MemberNameValidator.GenerateIdName("Set" + generatorColumnPropNameInRow + "Null", this.codeGenerator.CodeProvider, false);
                    CodeMemberMethod method2 = CodeGenHelper.MethodDecl(CodeGenHelper.GlobalType(typeof(void)), name, MemberAttributes.Public | MemberAttributes.Final);
                    method2.Statements.Add(CodeGenHelper.Assign(CodeGenHelper.Indexer(CodeGenHelper.This(), CodeGenHelper.Property(CodeGenHelper.Field(CodeGenHelper.This(), generatorTableVarName), generatorColumnPropNameInTable)), CodeGenHelper.Field(CodeGenHelper.GlobalTypeExpr(typeof(Convert)), "DBNull")));
                    rowClass.Members.Add(method2);
                }
            }
        }
コード例 #2
0
        private static CodeExpression BuildNewUnknownParameterStatements(DesignParameter parameter, Type parameterType, DbProviderFactory factory, IList statements, CodeExpression parameterVariable)
        {
            if (!ParamVariableDeclared(statements))
            {
                statements.Add(CodeGenHelper.VariableDecl(CodeGenHelper.GlobalType(parameterType), "param", CodeGenHelper.New(CodeGenHelper.GlobalType(parameterType), new CodeExpression[0])));
                parameterVariable = CodeGenHelper.Variable("param");
            }
            else
            {
                if ((parameterVariable == null) || !(parameterVariable is CodeVariableReferenceExpression))
                {
                    parameterVariable = CodeGenHelper.Variable("param");
                }
                statements.Add(CodeGenHelper.Assign(parameterVariable, CodeGenHelper.New(CodeGenHelper.GlobalType(parameterType), new CodeExpression[0])));
            }
            IDbDataParameter parameter2 = (IDbDataParameter)Activator.CreateInstance(parameterType);

            statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "ParameterName"), CodeGenHelper.Str(parameter.ParameterName)));
            if (parameter.DbType != parameter2.DbType)
            {
                statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "DbType"), CodeGenHelper.Field(CodeGenHelper.GlobalTypeExpr(typeof(DbType)), parameter.DbType.ToString())));
            }
            PropertyInfo providerTypeProperty = ProviderManager.GetProviderTypeProperty(factory);

            if (((providerTypeProperty != null) && (parameter.ProviderType != null)) && (parameter.ProviderType.Length > 0))
            {
                object obj2 = null;
                try
                {
                    obj2 = Enum.Parse(providerTypeProperty.PropertyType, parameter.ProviderType);
                }
                catch
                {
                }
                if (obj2 != null)
                {
                    statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, providerTypeProperty.Name), CodeGenHelper.Field(CodeGenHelper.TypeExpr(CodeGenHelper.GlobalType(providerTypeProperty.PropertyType)), obj2.ToString())));
                }
            }
            if (parameter.Size != parameter2.Size)
            {
                statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "Size"), CodeGenHelper.Primitive(parameter.Size)));
            }
            if (parameter.Direction != parameter2.Direction)
            {
                statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "Direction"), CodeGenHelper.Field(CodeGenHelper.GlobalTypeExpr(typeof(ParameterDirection)), parameter.Direction.ToString())));
            }
            if (parameter.IsNullable != parameter2.IsNullable)
            {
                statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "IsNullable"), CodeGenHelper.Primitive(parameter.IsNullable)));
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(persistScaleAndPrecisionRegistryKey))
            {
                if (key != null)
                {
                    if (parameter.Precision != parameter2.Precision)
                    {
                        statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "Precision"), CodeGenHelper.Primitive(parameter.Precision)));
                    }
                    if (parameter.Scale != parameter2.Scale)
                    {
                        statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "Scale"), CodeGenHelper.Primitive(parameter.Scale)));
                    }
                }
            }
            if (parameter.SourceColumn != parameter2.SourceColumn)
            {
                statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "SourceColumn"), CodeGenHelper.Str(parameter.SourceColumn)));
            }
            if (parameter.SourceVersion != parameter2.SourceVersion)
            {
                statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "SourceVersion"), CodeGenHelper.Field(CodeGenHelper.GlobalTypeExpr(typeof(DataRowVersion)), parameter.SourceVersion.ToString())));
            }
            if ((parameter2 is DbParameter) && (parameter.SourceColumnNullMapping != ((DbParameter)parameter2).SourceColumnNullMapping))
            {
                statements.Add(CodeGenHelper.Assign(CodeGenHelper.Property(parameterVariable, "SourceColumnNullMapping"), CodeGenHelper.Primitive(parameter.SourceColumnNullMapping)));
            }
            return(parameterVariable);
        }