简体   繁体   English

从模板在 C# 中自动生成的代码

[英]Auto-generated code in C# from a template

I am now maintaining someone else's code in C# and I have found something that I am not sure I understand.我现在正在用 C# 维护别人的代码,但我发现了一些我不确定我是否理解的东西。

So far when I program Winforms I usually write my classes (and other code).到目前为止,当我编写 Winforms 时,我通常会编写我的类(和其他代码)。 However in this program there are some modules that have partial classes and in the header there is a commentary of the form然而,在这个程序中有一些模块有部分类,并且在标题中有一个形式的评论

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

What are this exactly and what template are they talking about?这到底是什么,他们在谈论什么模板

(*) This are not the auto-generated partial classes generated when someone design graphically a form. (*) 这不是某人以图形方式设计表单时生成的自动生成的部分类。 I have seen and used those.我见过并使用过这些。 I suppose it is something similar but not the same我想这是类似但不一样的东西

The comments below your question point you in the right direction for the specific case at hand but it is interesting to think about what is the general best practice when working on a project with auto generated code that is hopefully marked as such by a comment.您的问题下方的评论为您手头的特定案例指明了正确的方向,但考虑一下在使用自动生成的代码(希望通过评论标记为此类)的项目上工作时的一般最佳实践是什么是很有趣的。

Well such a comment is telling you that this is not actual source code, but code that has been transpiled/generated from another source .好吧,这样的评论告诉您这不是实际的代码,而是从另一个转换/生成的代码 It is indeed not the best idea to modify it directly as the process generating that code (most likely as part of the build or using a tool) can discard your changes by overwriting the file.直接修改它确实不是最好的主意,因为生成该代码的过程(很可能作为构建的一部分或使用工具)可以通过覆盖文件来丢弃您的更改。 One way to think of that is that it is a bit like if this was binary generated by the compiler (a more correct term would be intermediate language).一种思考方式是,这有点像编译器生成的二进制文件(更正确的术语是中间语言)。 Same would apply in that case, modifying the binary would be counter productive, error prone and the changes would be discarded by the actual compilation process.在这种情况下同样适用,修改二进制文件会适得其反,容易出错,并且更改将被实际编译过程丢弃。

Moreover that kind of code usually follows strict conventions and is expected to work if those conventions are scrupulously respected.此外,这种代码通常遵循严格的约定,如果严格遵守这些约定,则有望工作。 One thing to have in mind is that usually such auto generated code is dumb code that is better left to a tool to be generated.要记住的一件事是,通常这种自动生成的代码是愚蠢的代码,最好留给要生成的工具。

From time to time I write tools to generate code automatically, this can be very useful for things like glue code (mapping to a database, serialization, etc..).我有时会编写工具来自动生成代码,这对于粘合代码(映射到数据库、序列化等)之类的事情非常有用。 Automatically generated tests etc.. One particular example that I remember having used in real production code is stubbing .自动生成的测试等。我记得在实际生产代码中使用过的一个特定示例是stubbing We had a wpf client that was plagged with null reference exceptions.我们有一个带有空引用异常的 wpf 客户端。 My team was spending too much time fixing them case by case but more were appearing all the time.我的团队花了太多时间逐个修复它们,但一直有更多的问题出现。 At some point I implemented a configurable tool that would produce code that patch the data model putting default stub objects in place of null references.在某个时候,我实现了一个可配置的工具,该工具将生成修补数据模型的代码,将默认存根对象替换为空引用。

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

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