天天看點

通過代碼生成機制實作強類型程式設計-CodeSimth版

        好直接到主題。首先是資料實體messageentry(我到老a的基礎上添加了description屬性作為代碼字段描述):

通過代碼生成機制實作強類型程式設計-CodeSimth版
通過代碼生成機制實作強類型程式設計-CodeSimth版

 1 namespace wolf 

 2 { 

 3     public class messageentry 

 4     { 

 5         public string id { get; private set; } 

 6         public string value { get; private set; }        

 7         public string description { get; private set; } 

 8         public messageentry(string id, string value) 

 9         { 

10             this.id = id; 

11             this.value = value;         

12         } 

13 

14         public messageentry(string id, string value, string description) 

15         { 

16             this.id = id; 

17             this.value = value; 

18             this.description = description; 

19         } 

20 

21         public string format(params object[] args) 

22         { 

23             return string.format(this.value, args); 

24         } 

25     } 

26 }

27 

28 

通過代碼生成機制實作強類型程式設計-CodeSimth版

在我的機子上的codesimth是2..0版本的是以不能使用linq命名空間,我又想利用這個空間,比較快捷,是以我就在先3.0轉化為

dictionary<string, list<messageentry>>實體再傳入模闆:

code:

通過代碼生成機制實作強類型程式設計-CodeSimth版
通過代碼生成機制實作強類型程式設計-CodeSimth版

 1 using system; 

 2 using system.collections.generic; 

 3 using system.linq; 

 4 using system.text; 

 5 using system.xml; 

 6 using system.xml.linq; 

 7 

 8 namespace wolf 

 9 { 

10     public class messagecodegenerator 

11     { 

12         public dictionary<string, list<messageentry>> generatorcode(string  path) 

13         { 

14             return generatorcode(xelement.load(path)); 

15         } 

16         public dictionary<string, list<messageentry>> generatorcode(xelement root) 

17         { 

18 

19             var elemts = root.elements("message").groupby(e => ((xattribute)e.attribute("category")).value); 

20             dictionary<string, list<messageentry>> dict = new dictionary<string, list<messageentry>>(); 

21             foreach (var item in elemts) 

22             { 

23                 list<messageentry> list = new list<messageentry>(); 

24                 foreach (var g in item) 

25                 { 

26                     if (g.attribute("description") != null) 

27                     { 

28                         list.add(new messageentry(((xattribute)g.attribute("id")).value, ((xattribute)g.attribute("value")).value, ((xattribute)g.attribute("description")).value)); 

29                     } 

30                     else 

31                     { 

32                         list.add(new messageentry(((xattribute)g.attribute("id")).value, ((xattribute)g.attribute("value")).value)); 

33                     } 

34 

35                 } 

36                 dict.add(item.key.tostring(), list); 

37             } 

38             return dict; 

39 

40         } 

41     } 

42 }

43 

44 

通過代碼生成機制實作強類型程式設計-CodeSimth版

這下幾可開始寫模闆了,見下code:

通過代碼生成機制實作強類型程式設計-CodeSimth版
通過代碼生成機制實作強類型程式設計-CodeSimth版

 1 <%@ codetemplate language="c#" targetlanguage="text" src="" inherits="" debug="false" description="template description here." %> 

 2 

 3 <%@ import namespace="system" %> 

 4 <%@ import namespace="system.xml" %> 

 5 <%@ import namespace="system.text" %> 

 6 <%@ import namespace="system.collections.generic" %> 

 7 <%@ import namespace="wolf" %> 

 8 <%@ assembly name="wolf" %> 

 9 

10 <script runat="template"> 

11 

12 private dictionary<string, list<messageentry>> dict; 

13 public dictionary<string, list<messageentry>> generator 

14 { 

15     get 

16     { 

17         return dict; 

18     } 

19     set 

20     { 

21         dict=value; 

22     } 

23 } 

24 

25 public string generatorcode() 

26         {            

27             string str = "using wolf;\r\nusing system;\r\nusing system.collections.generic;\r\nnamespace wolf.message\r\n{ \r\n   public class messages\r\n    {\r\n"; 

28             foreach (string catage in generator.keys) 

29             { 

30                 str += "        public class  "+catage + "\r\n        {        \r\n"; 

31                 foreach (wolf.messageentry entry in generator[catage]) 

32                 { 

33                     str += "\r\n            /// <summary>" + 

34                     "\r\n            ///" + entry.description + 

35                     "\r\n            /// </summary>" + 

36                     "\r\n            public static wolf.messageentry " + entry.id + " = new messageentry(\"" + entry.id + "\", \"" + entry.value +"\", \"" + entry.value +"\");\r\n"; 

37                 } 

38                 str += "\r\n        }\r\n\r\n"; 

40             } 

41             str += "\r\n    }\r\n}"; 

42             return str; 

43         } 

45 </script> 

46 //copyright (c) wolf.  all rights reserved. 

47 <%=  generatorcode()%>

48 

49 

通過代碼生成機制實作強類型程式設計-CodeSimth版

很簡單,就不說了,如果有問題請留言,其中命名空間完全可以以屬性方式傳入。

xml實體用的是老a的:

通過代碼生成機制實作強類型程式設計-CodeSimth版
通過代碼生成機制實作強類型程式設計-CodeSimth版

代碼

1 <?xml version="1.0" encoding="utf-8" ?>  

2 <messages>   

3 <message id="mandatoryfield" value="the {0} is mandatory."  category="validation"  description="description" />   

4 <message id="greaterthan" value="the {0} must be greater than {1}."  category="validation" description="description" />   

5 <message id="reallydelete" value="do you really want to delete the {0}."  category="confirmation" description="description" />  

6 </messages>

通過代碼生成機制實作強類型程式設計-CodeSimth版

我想脫離codesimth工具,是以在建立了一個控制台程式,引用codesmith.engine.dll程式集。

通過代碼生成機制實作強類型程式設計-CodeSimth版
通過代碼生成機制實作強類型程式設計-CodeSimth版

 1 static void main(string[] args) 

 2        { 

 3           // wolf.message.messages.confirmation.reallydelete.value 

 4            // test(); 

 5            codetemplate template = compiletemplate(@"e:\myapp\linqtest\consoleapplication1\messagecodegenerator.cst",s=>console.writeline(s)); 

 6            if (template != null) 

 7            { 

 8                template.setproperty("_messagefilepath", ""); 

 9                wolf.messagecodegenerator gen = new messagecodegenerator(); 

10                dictionary<string, list<messageentry>> dict = gen.generatorcode(@"e:\myapp\linqtest\consoleapplication1\sample.xml"); 

11                template.setproperty("generator", dict); 

12                template.rendertofile("gen.cs", true); 

13               // system.diagnostics.process.start("gen.cs"); 

14            } 

15           // console.read(); 

16 

17        }

19        public static codetemplate compiletemplate(string templatename,action<string> errorwriter) 

20        { 

21            codetemplatecompiler compiler = new codetemplatecompiler(templatename); 

22            compiler.compile(); 

23 

24            if (compiler.errors.count == 0) 

25            { 

26                return compiler.createinstance(); 

27            } 

28            else 

29            { 

30                for (int i = 0; i < compiler.errors.count; i++) 

31                { 

32                    errorwriter(compiler.errors[i].tostring()); 

33                } 

34                return null; 

35            } 

36 

37        }

38 

通過代碼生成機制實作強類型程式設計-CodeSimth版

生成後的代碼:

通過代碼生成機制實作強類型程式設計-CodeSimth版
通過代碼生成機制實作強類型程式設計-CodeSimth版

 1 //copyright (c) wolf.  all rights reserved. 

 2 using wolf; 

 3 using system; 

 4 using system.collections.generic; 

 5 namespace wolf.message 

 6 { 

 7     public class messages 

 8     { 

 9         public class validation 

10         { 

12             /// <summary> 

13             ///description 

14             /// </summary> 

15             public static wolf.messageentry mandatoryfield = new messageentry("mandatoryfield", 

17 "the {0} is mandatory.", "the {0} is mandatory."); 

19             /// <summary> 

20             ///description 

21             /// </summary> 

22             public static wolf.messageentry greaterthan = new messageentry("greaterthan", 

24 "the {0} must be greater than {1}.", "the {0} must be greater than {1}."); 

25 

26         } 

28         public class confirmation 

29         { 

30 

31             /// <summary> 

32             ///description 

33             /// </summary> 

34             public static wolf.messageentry reallydelete = new messageentry("reallydelete", 

35 

36 "do you really want to delete the {0}.", "do you really want to delete the {0}."); 

37 

38         } 

40     } 

41 }

42 

通過代碼生成機制實作強類型程式設計-CodeSimth版