简体   繁体   English

IEnumerable T4模板

[英]IEnumerable T4 Templates

I'm working on some code-generation stuff - using T4 RuntimTemplates. 我正在研究一些代码生成的东西-使用T4 RuntimTemplates。 I do have more than one template and I "store" them in a IEnumberable List. 我确实有多个模板,并且将它们“存储”在IEnumberable列表中。

My problem is that I want to iterate through the templates using .WriteAllText() which is a virtual Method. 我的问题是我想使用.WriteAllText()这是一个虚拟方法来遍历模板。

I thought, that it might be the easiest way to create an Interface with the definition of .WriteAllText() an declare it in the partial class.. 我认为,这可能是创建带有.WriteAllText()定义并在局部类中声明它的接口的最简单方法。

    public partial class SolutionTemplateRunTime :     SolutionTemplateRunTimeBase, ICodegenerationTemplate
{

This works till I store or update the template ;-) . 在我存储或更新模板之前,这一直有效;-)。 The partial class (the code behind cs) of the template gets updated and the interfaces declaration is gone. 模板的部分类(cs后面的代码)将更新,并且接口声明不见了。

    public partial class SolutionTemplateRunTime :     SolutionTemplateRunTimeBase
{

Is there a solution to handle that? 有解决方案吗? Its kind of annoying to redeclare the interface whenever I update the template.. Hope you can help.. 每当我更新模板时都会重新声明接口,这很烦人。希望您能提供帮助。

Greetz Iki 格雷茨·伊基(Greetz Iki)

I think you're missing the point of partial - the idea is that exactly the kind of changes you want to do are done in a separate file - one that isn't generated. 我认为您缺少partial -这个想法是,您要进行的更改的确切类型是在单独的文件中完成的-不会生成该文件。

Just create a new cs file, with a declaration like this: 只需创建一个新的cs文件,并带有如下声明:

public partial class SolutionTemplateRunTime : ICodegenerationTemplate
{
  ...
}

When compiling the code, the compiler will merge all partial declarations of the same class - this includes whatever interfaces the class implements etc. 编译代码时,编译器将合并同一类的所有部分声明-这包括该类实现的接口等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM