使用模板动态生成代码

时间:2020-02-20 10:31:47

标签: c# templates text

从模板动态生成Code时,我正在寻求帮助。

模板将填充有以前需要替换占位符的导入数据。

编辑:不需要编译生成的代码。它将在另一个程序中使用。生成由用户输入触发。

这里的问题是,例如可以有多个属性。属性需要再次拥有自己的模板。这又可能包含注释模板的关键字,依此类推(没有循环可能)。注释绑定到属性或类本身,这意味着必须有一种将两者分开的方法。

这是我正在使用的自定义模板: Edit2:这只是一个示例,生成的代码不必是C#,它可以是用户选择作为模板的任何内容。

###File
using System;

namespace {Namespace}
{
    internal class {Classname}{Base}{Comment}
    {
        //Basic Attributes
{Attributes}

        //Structured Attributes
{Structuredattributes}
    }
}
###

###Attributes
        internal {Datatype} {Attributename} { get; set; }{Comment}      
###

###Base
 : {Basename}
###

###Comment
 // {Commentdata}
###

说明:

{Word} is a placeholder. All Placeholders are predefined (for now)
###Word is the start, Word is the ID. That means this block defines a placeholders look.
### is the end

目前,我正在使用Replace(Placeholder,data)-功能,但这似乎不是一个很好的解决方案。

我需要一个模板,以便用户可以简单地编写新的Exportformat,而无需完全接触代码。 用户也可以完全定义外观。所有的空格和线条也将被放置。

也许有人有一个好主意,可以将我推向正确的方向。我不介意更改模板格式,但是我需要很好的可读性。

谢谢!

0 个答案:

没有答案