예제 #1
0
        private static void CreateConstructorFromTable(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeConstructor()
            {
                Attributes = MemberAttributes.Public
            };

            cc.Parameters.Add(generationInfo.ReaderNamespace, "ITable", "table");
            cc.BaseConstructorArgs.Add("table");

            // verify revision
            {
                var css = CreateCompareTypeStatement(classType, tableInfo, generationInfo);
                var tst = CremaDataClassCreator.CreateTryCatchStatement(classType, css, generationInfo);
                cc.Statements.Add(tst);
            }

            // initialize childs
            {
                var table   = new CodeVariableReferenceExpression("table");
                var dataSet = new CodePropertyReferenceExpression(table, "DataSet");
                var tables  = new CodePropertyReferenceExpression(dataSet, "Tables");

                foreach (var item in generationInfo.GetChilds(tableInfo))
                {
                    var tableName  = new CodePropertyReferenceExpression(table, "Name");
                    var childName  = new CodeBinaryOperatorExpression(tableName, CodeBinaryOperatorType.Add, new CodePrimitiveExpression("." + item.TableName));
                    var childTable = new CodeIndexerExpression(tables, childName);
                    var childField = item.GetFieldExpression();
                    var instance   = new CodeObjectCreateExpression(item.GetCodeType(), childTable);

                    cc.Statements.AddAssign(childField, instance);
                }
            }

            // SetRelations
            {
                var table = new CodeVariableReferenceExpression("table");
                foreach (var item in generationInfo.GetChilds(tableInfo))
                {
                    var setRelations   = new CodeMethodReferenceExpression(thisRef, "SetRelations");
                    var rows           = new CodePropertyReferenceExpression(item.GetFieldExpression(), "Rows");
                    var setChildAction = new CodePropertyReferenceExpression(tableInfo.GetRowCodeTypeExpression(), "Set" + item.TableName);
                    var tableName      = new CodePropertyReferenceExpression(table, "Name");
                    var childName      = new CodeBinaryOperatorExpression(tableName, CodeBinaryOperatorType.Add, new CodePrimitiveExpression("." + item.TableName));

                    var setRelationsInvoke = new CodeMethodInvokeExpression(setRelations, childName, rows, setChildAction);
                    cc.Statements.Add(setRelationsInvoke);
                }
            }

            classType.Members.Add(cc);
        }
예제 #2
0
        private string GenerateTableCode(CodeGenerationInfo generationInfo)
        {
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                var codeDomProvider = new CSharpCodeProvider();
                var codeGenerator   = codeDomProvider.CreateGenerator(sw);
                var compileUnit     = new CodeCompileUnit();
                var codeNamespace   = new CodeNamespace(generationInfo.Namespace);
                codeNamespace.Imports.Add(new CodeNamespaceImport(generationInfo.ReaderNamespace));
                CremaDataClassCreator.Create(codeNamespace, generationInfo);
                compileUnit.Namespaces.Add(codeNamespace);
                codeGenerator.GenerateCodeFromCompileUnit(compileUnit, sw, this.options);
            }

            return(sb.ToString());
        }