/// <summary> /// 從程式碼讀出 class 的結構 /// </summary> public static CsSchema.Class AnalysisClass(string programText) { CsSchema.Class csClass = new CsSchema.Class(); List <CsSchema.Property> csProperties = new List <CsSchema.Property>(); SyntaxTree tree = CSharpSyntaxTree.ParseText(programText); CompilationUnitSyntax root = tree.GetRoot() as CompilationUnitSyntax; ClassDeclarationSyntax classSyntax = root.DescendantNodes().OfType <ClassDeclarationSyntax>().Single(); IEnumerable <PropertyDeclarationSyntax> propertiesSyntax = classSyntax.DescendantNodes().OfType <PropertyDeclarationSyntax>(); foreach (var propertySyntax in propertiesSyntax) { PredefinedTypeSyntax typeSyntax = propertySyntax.DescendantNodes().OfType <PredefinedTypeSyntax>().Single(); SyntaxToken type = typeSyntax.Keyword; SyntaxToken property = propertySyntax.Identifier; csProperties.Add(new CsSchema.Property() { Name = property.Text, TypeName = type.Text }); } csClass.Name = classSyntax.Identifier.Text; csClass.Properties = csProperties.ToArray(); return(csClass); }
public async Task <Response> Handle(Request request, CancellationToken token) { CsSchema.Class csClass = new CsSchema.Class("Project"); csClass.Access = CsSchema.Access.Public; csClass.Fields = new CsSchema.Field[] { new CsSchema.Field("bool", "isAA") }; csClass.Properties = new CsSchema.Property[] { new CsSchema.Property("string", "ppp1") { Attributes = new CsSchema.Attribute[] { new CsSchema.Attribute("Key"), new CsSchema.Attribute("Column") } } }; csClass.Attributes = new CsSchema.Attribute[] { new CsSchema.Attribute("Table", "C1", "QQ = \"X\"", "Z = 123") }; CsSchema.Unit unit = new CsSchema.Unit(); CsSchema.Namespace @namespace = new CsSchema.Namespace(); @namespace.Name = "TestProject"; @namespace.Classes = new CsSchema.Class[] { csClass }; unit.Usings = new string[] { "System", "System.Collections.Generic" }; unit.Namespaces = new CsSchema.Namespace[] { @namespace }; var syntax = mapper.Map <CompilationUnitSyntax>(unit); return(new Response { Code = syntax .NormalizeWhitespace() .ToFullString() }); }
public async Task <Response> Handle(Request request, CancellationToken token) { string connectionString = "Server=LAPTOP-RD9P71LP\\SQLEXPRESS;Database=TEST1;UID=sa;PWD=1234;"; DbSchema.Table tableSchema = CodingHelper.GetDbTableSchema(connectionString, "Article"); CsSchema.Class csClass = mapper.Map <CsSchema.Class>(tableSchema); CsSchema.Unit unit = new CsSchema.Unit(); CsSchema.Namespace @namespace = new CsSchema.Namespace(); @namespace.Name = "QQ"; @namespace.Classes = new CsSchema.Class[] { csClass }; unit.Namespaces = new CsSchema.Namespace[] { @namespace }; var syntax = mapper.Map <CompilationUnitSyntax>(unit); return(new Response { Code = syntax .NormalizeWhitespace() .ToFullString() }); }