代码生成技术

代码生成技术

目标

通过xml文件的配置生成C#代码.

示例

1. 编辑xml文件

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

<dataentitytypes>

  <dataentitytype domain="database">

    <name>School</name>

    <properties>

      <simpleproperties>

        <simpleproperty name="Id" dbtype="string">

          <description>id</description>

        </simpleproperty>

        <simpleproperty name="Name" dbtype="string">

          <description>name</description>

        </simpleproperty>

      </simpleproperties>

      <collectionproperties>

        <collectionproperty name="Classes">

          <itemdataentitytypename>Class</itemdataentitytypename>

        </collectionproperty>

      </collectionproperties>

    </properties>

  </dataentitytype>

  <dataentitytype>

    <name>Class</name>

    <properties>

      <simpleproperties>

        <simpleproperty name="Id" dbtype="string">

          <description>id</description>

        </simpleproperty>

        <simpleproperty name="Name" dbtype="string">

          <description>name</description>

        </simpleproperty>

        <simpleproperty name="Member" dbtype="int">

          <description>member</description>

        </simpleproperty>

      </simpleproperties>

    </properties>

  </dataentitytype>

</dataentitytypes>

2. 编辑xslt文件

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"

>

  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">

    /*

    *auto-generated

    */

    using System;

    using System.Collections.Generic;

    namespace DataEntity

    {

        <xsl:for-each select="dataentitytypes/dataentitytype">

        public sealed class <xsl:call-template name="classname"></xsl:call-template>

        {

            <xsl:call-template name="simpleproperties"></xsl:call-template>

            <xsl:call-template name="collectionproperties"></xsl:call-template>

        }

        </xsl:for-each>

    }

  </xsl:template>

  <xsl:template name="classname">

    <xsl:value-of select="name"/>

  </xsl:template>

  <xsl:template name="simpleproperties">

    <xsl:for-each select="properties/simpleproperties/simpleproperty">

            public <xsl:value-of select="@dbtype"/><xsl:text> </xsl:text><xsl:value-of select="@name"/>

            {

                get;

                set;

            }

    </xsl:for-each>

  </xsl:template>

  <xsl:template name="collectionproperties">

    <xsl:for-each select="properties/collectionproperties/collectionproperty">

            public IList &lt;<xsl:value-of select="itemdataentitytypename"/>&gt;<xsl:text> </xsl:text><xsl:value-of select="@name"/>

            {

                get;

                set;

            }

    </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>

小技巧:Xslt文件编写之后是可以在VS中调试和显示输出的,点击XSLT文件,然后VS菜单栏会多出一个XML的菜单,选择即可!

3. 编写转换类

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Xml.Xsl;

namespace XML

{

    public class DataEntityGenerator

    {

        public static void Compile(string stylesheetUri,string inputUri,string outputFile)

        {

            XslCompiledTransform transform = new XslCompiledTransform(true);

            transform.Load(stylesheetUri);

            transform.Transform(inputUri, outputFile);

        }

    }

}

4. 运行结果

    /*

    *auto-generated

    */

    using System;

    using System.Collections.Generic;

    namespace DataEntity

    {

        

        public sealed class School

        {

            

            public string Id

            {

                get;

                set;

            }

    

            public string Name

            {

                get;

                set;

            }

    

            public IList<Class> Classes

            {

                get;

                set;

            }

    

        }

        

        public sealed class Class

        {

            

            public string Id

            {

                get;

                set;

            }

    

            public string Name

            {

                get;

                set;

            }

    

            public int Member

            {

                get;

                set;

            }

    

        }

        

    }

小结

本文仅仅是介绍一种简单的代码生成技术.如果按照此方法设计代码生成器,最好对XML,XPath,XSD,XSL,XSLT以及IO流有一定的了解.所有的XML相关的文档的知识都可以在http://www.w3school.com.cn查到,与XML相关的类库主要分布在System.Xml程序集中(对xml可以使用LinqToXml技术),除此之外,XML相关的类与IO中的许多流也有密切联系.

作者:zhoulq

原文地址:https://www.cnblogs.com/kingge/p/1923906.html