DCG (Dynamic Code Generator)

xiaoxiao2021-03-06  50

How do I always write blog on Tuesday recently? Take a look at the calendar on the blog, all written on Tuesday! ^ _ ^ Speech, recently written another small "item", dynamic code generator for a project, and dynamic code generator. In fact, it is a dynamic template, a template with a similar ASP.NET syntax, should be said to be the same as CODesmith, but now there is no editor. Currently, a DLL can provide all the information you need to build a template editor, including the compile time error of the dynamic template, and the runtime error. I used a lot of new knowledge learned in this small project, and the designs used in this small project were the best use of the best. ^ _ ^ Describes the syntax and method of using the template:

<% @ Template Language = "C #" Name = "" Version = ""%>: The template starts, you must have this template is a valid template, its language attribute is required, the dynamic language used in the specified template Type, currently support C # and VB.NET. <% @ Parameter name = "paramname" DATATYPE = "system.string" assemblyname = "": template parameters, all of which must be required, need to be Mscorlib.dll when the parameter type is set AssemblyName property is set to empty. A template can have multiple parameters. <% Code%>: Dynamic code in the template, in which the language can be written to dynamic code in the language specified by the template Language property. <% = variable%>: The dynamic code in the template returns the result of the dynamic code. Note that if the dynamic code does not return any results, then the compilation error will be generated. <% # void method ()%>: Dynamic method, can simply define dynamic methods in the template so that this dynamic method can be called directly in other dynamic code parts. <% - Comment -%>: Note in Dynamic Templates. The template itself is not Case Sensitive, but the value in each TAG property is based on the language used by the template to be Case Sensitive. For example, if it is C #, then Case Sensitive, if it is vb.net, it is not. The following is an example of C #: <% @ template name = "Sample1" version = "1.0" language = "c #"%> <% @ parameter name = "Entity" datatype = "osit" assemblyName = "DCGTEST .exe "%> using system; // This is a Chinese comment.

<% - Template Note -%> Namespace <% = Entity.Namespace%> {Public Sealed Class <% = Entity.Name%> {public static void callme () {arraylist list = new arraylist (); <% for (INT i = 1; i <= 10; i ) {%> list.add ("<% = getentityName () i%>"); <%}%>}}} <% # string gelectrityName () { Return Entity.getFullName ();}%> Call template code is: string filecontent = this.readfile (Application.Startuppath // " " Sample Template.dt "); Entity Ent = New Entity (); ENT. namespace = "SampleNamespace"; ent.Name = "SampleEntity"; ITemplateManager templateManager = new TemplateManager (); try {ITemplate template = templateManager.NewTemplate (fileContent); this.textBox1.SelectedText = "This template's name is:" template .Name Environment.NewLine; this.textBox1.SelectedText = "This template's language is:" template.Language Environment.NewLine Environment.NewLine; this.textBox1.SelectedText = template.GenerateCode (new object [] { ENT});} catch (CompileTimeException EX) {This.textBox1.Clear (); foreach (CompilerError error in ex.CompilerResults.Errors) {this.textBox1.SelectedText = error.ErrorText Environment.NewLine;} return;} catch (RunTimeException ex) {this.textBox1. Clear (); this.textBox1.SelectedText = ex.InnerException.InnerException.ToString () Environment.NewLine; return;} catch (TemplateException ex) {this.textBox1.Clear (); this.textBox1.SelectedText = ex .ToString () Environment.newline; Return;

} The result is: This Template's name is: sample1this template's language is: c # using system; // This is a Chinese. namespace SampleNamespace {public sealed class SampleEntity {public static void CallMe () {ArrayList list = new ArrayList (); list.Add ( "SampleNamespace.SampleEntity1"); list.Add ( "SampleNamespace.SampleEntity2"); list.Add ( " SampleNamespace.SampleEntity3 "); list.Add (" SampleNamespace.SampleEntity4 "); list.Add (" SampleNamespace.SampleEntity5 "); list.Add (" SampleNamespace.SampleEntity6 "); list.Add (" SampleNamespace.SampleEntity7 "); List.add ("SampleNameSpace.SAMpleEntity); list.add (" SampleNameSpace.SAMpleEntity); list.add ("SampleNameSpace.SAMpleEntity10);}}} It is very simple to see from the code, and you can Very easy integration in any project, this has reached my initial purpose. CODesmith can do this, but it is not easy to integrate into other projects. Such a dynamic code generator is very wide, but I want to see the readers of this article to give me better opinions, or discuss, to make DCG more perfect! ^ _ ^ Author BLOG:

http://blog.9cbs.net/cavingdeep/

related articles

DCG is upgraded! DCG (Dynamic Code Generator) Mode and Design Mode Overview The least responsibilities OOD design means OOD design method summary

转载请注明原文地址:https://www.9cbs.com/read-89581.html

New Post(0)