public string Save(string path, cfLangType clt, KeelKit.Generators.BaseGengerator.DGetFileNames getfilename) { CodeDomProvider provider = BaseGengerator.GetProvider(clt); string filename = getfilename != null?getfilename(path, provider.FileExtension) : GetFileNames(path, provider.FileExtension); IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(filename, false, Encoding.UTF8), " "); provider.GenerateCodeFromCompileUnit(ccu, tw, new CodeGeneratorOptions()); tw.Close(); string context = System.IO.File.ReadAllText(filename, Encoding.UTF8); if (!context.Contains("<auto-generated>") && ccu.Namespaces.Count > 0) { ccu.Namespaces[0].Comments.Clear(); ccu.Namespaces[0].Comments.Add(new CodeCommentStatement(BaseGengerator.AddKeelKitInfo())); tw = new IndentedTextWriter(new StreamWriter(filename, false, Encoding.UTF8), " "); provider.GenerateCodeFromCompileUnit(ccu, tw, new CodeGeneratorOptions()); tw.Close(); } return(filename); }
public void Gengerator(string cfNameSpace, string[] tables) { #region 生成命名空间引用 cns = new CodeNamespace(cfNameSpace); cns.Imports.Add(new CodeNamespaceImport("System")); cns.Imports.Add(new CodeNamespaceImport("System.Collections.Generic")); cns.Imports.Add(new CodeNamespaceImport("System.ComponentModel")); cns.Imports.Add(new CodeNamespaceImport("System.Data")); cns.Imports.Add(new CodeNamespaceImport("System.Text")); cns.Imports.Add(new CodeNamespaceImport("Keel")); cns.Comments.Add(new CodeCommentStatement(BaseGengerator.AddKeelKitInfo(false))); #endregion #region 生成类头部 ctd = new CodeTypeDeclaration("KeelEntities"); ctd.IsClass = true; ctd.Attributes = (ctd.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private; ctd.IsClass = true; CodeTypeReference _KeelContext_type1 = new CodeTypeReference(typeof(Keel.KeelContext)); ctd.BaseTypes.Add(_KeelContext_type1); #endregion #region 生成类的实例化部分 // // Field idb // CodeMemberField _idb_field1 = new CodeMemberField(); _idb_field1.Attributes = (_idb_field1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private; _idb_field1.Name = "idb"; CodeTypeReference _IDatabase_type1 = new CodeTypeReference("IDatabase"); _idb_field1.Type = _IDatabase_type1; ctd.Members.Add(_idb_field1); // // Field dbo // CodeMemberField _dbo_field1 = new CodeMemberField(); _dbo_field1.Attributes = (_dbo_field1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private; _dbo_field1.Name = "dbo"; CodeTypeReference _DBOperator1_type1 = new CodeTypeReference("DBOperator"); _DBOperator1_type1.TypeArguments.Add(typeof(Keel.IDatabase)); _dbo_field1.Type = _DBOperator1_type1; ctd.Members.Add(_dbo_field1); // // Constructor() // CodeConstructor __ctor_ctor1 = new CodeConstructor(); __ctor_ctor1.Attributes = (__ctor_ctor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; CodePropertyReferenceExpression _prop1 = new CodePropertyReferenceExpression(); _prop1.PropertyName = "NowDataBase"; CodeTypeReferenceExpression _ref1 = new CodeTypeReferenceExpression(); CodeTypeReference _Keel_DB_Common_type1 = new CodeTypeReference(typeof(Keel.DB.Common)); _ref1.Type = _Keel_DB_Common_type1; _prop1.TargetObject = _ref1; __ctor_ctor1.ChainedConstructorArgs.Add(_prop1); ctd.Members.Add(__ctor_ctor1); // // Constructor(IDatabase _idb) // CodeConstructor __ctor_ctor2 = new CodeConstructor(); __ctor_ctor2.Attributes = (__ctor_ctor2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; CodeTypeReference _IDatabase_type2 = new CodeTypeReference(typeof(Keel.IDatabase)); CodeParameterDeclarationExpression __idb_arg1 = new CodeParameterDeclarationExpression(_IDatabase_type2, "_idb"); __idb_arg1.Direction = FieldDirection.In; __idb_arg1.Name = "_idb"; CodeTypeReference _IDatabase_type3 = new CodeTypeReference(typeof(Keel.IDatabase)); __idb_arg1.Type = _IDatabase_type3; __ctor_ctor2.Parameters.Add(__idb_arg1); CodeAssignStatement _assign1 = new CodeAssignStatement(); CodeFieldReferenceExpression _field1 = new CodeFieldReferenceExpression(); _field1.FieldName = "idb"; CodeThisReferenceExpression _this1 = new CodeThisReferenceExpression(); _field1.TargetObject = _this1; _assign1.Left = _field1; CodeVariableReferenceExpression _arg1 = new CodeVariableReferenceExpression(); _arg1.VariableName = "_idb"; _assign1.Right = _arg1; __ctor_ctor2.Statements.Add(_assign1); ctd.Members.Add(__ctor_ctor2); // // Constructor(DBOperator`1 _odb) // CodeConstructor __ctor_ctor3 = new CodeConstructor(); __ctor_ctor3.Attributes = (__ctor_ctor3.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public; CodeTypeReference _DBOperator1_type2 = new CodeTypeReference("DBOperator"); _DBOperator1_type2.TypeArguments.Add(typeof(Keel.IDatabase)); CodeParameterDeclarationExpression __odb_arg1 = new CodeParameterDeclarationExpression(_DBOperator1_type2, "_odb"); __odb_arg1.Direction = FieldDirection.In; __odb_arg1.Name = "_odb"; CodeTypeReference _DBOperator1_type3 = new CodeTypeReference("DBOperator"); _DBOperator1_type3.TypeArguments.Add(typeof(Keel.IDatabase)); __odb_arg1.Type = _DBOperator1_type3; __ctor_ctor3.Parameters.Add(__odb_arg1); CodeAssignStatement _assign2 = new CodeAssignStatement(); CodeFieldReferenceExpression _field2 = new CodeFieldReferenceExpression(); _field2.FieldName = "dbo"; CodeThisReferenceExpression _this2 = new CodeThisReferenceExpression(); _field2.TargetObject = _this2; _assign2.Left = _field2; CodeVariableReferenceExpression _arg2 = new CodeVariableReferenceExpression(); _arg2.VariableName = "_odb"; _assign2.Right = _arg2; __ctor_ctor3.Statements.Add(_assign2); ctd.Members.Add(__ctor_ctor3); #endregion foreach (var item in tables) { AddItem(item); } cns.Types.Add(ctd); ccu.Namespaces.Add(cns); }