简体   繁体   English

如何识别由Visual Studio的GUI设计器生成的代码?

[英]How to recognize code generated by Visual Studio's GUI designer?

Visual Studio is kind enough to generate a lot of code for us when we create and design Windows.Forms controls. 当我们创建和设计Windows.Forms控件时,Visual Studio足以为我们生成很多代码。 It also surrounds most of it with a #region statement. 它还用#region语句包围了大部分内容。 In newer versions it also uses a partial class to separate generated from manually created code. 在较新的版本中,它也使用部分类将生成的代码与手动创建的代码分开。 Developers are supposed to edit code only in certain areas. 开发人员只应在某些区域编辑代码。

But nothing prevents us from violating this in whatever way we please. 但是,没有什么可以阻止我们以自己喜欢的任何方式违反这一规定。 I'm fine with manual edits that could just as well have been made from the designer, or manual edits in areas the designer doesn't touch. 我也可以由设计师进行手动编辑,也可以在设计师不接触的区域进行手动编辑。 But I'd like to flag any other kind of edit. 但我想标记其他任何类型的编辑。

Does anyone know a utility that can do this? 有谁知道可以执行此操作的实用程序? StyleCop rules perhaps? StyleCop规则? I mostly need it for the combination of C#, Windows.Forms, and Visual Studio 2003, 2005, and 2008. 我最需要C#,Windows.Forms和Visual Studio 2003、2005和2008的组合。

These days, designer code should end up in a .Designer.cs file. 这些天,设计器代码应最终以.Designer.cs文件结尾。 It should be very rare that developers need to touch that. 开发人员很少需要碰触它。 Unfortunately, I don't know any way of verifying that the code was genuinely generated by the designer. 不幸的是,我不知道有任何方法可以验证代码是否真正由设计人员生成。 It would be handy if it included some sort of hash, but it doesn't as far as I'm aware... 如果它包含某种哈希,那将很方便,但是据我所知……

Given how easy it is now to just say "don't edit designer files" do you really need another system though? 现在说“不编辑设计器文件”有多么容易,您是否真的需要另一个系统? It's not like you need to stay away from specific regions - it's the whole file which is out of bounds. 并不是您需要远离特定区域,而是整个文件超出范围。

Why should developers not be allowed to change this code? 为什么不允许开发人员更改此代码? If they are able to write code that works, they should be allowed to do it. 如果他们能够编写有效的代码,则应允许他们这样做。 If they are not able to write code that works, lets say they should be trained or fired. 如果他们不能编写有效的代码,可以说应该对其进行培训或开除。

You just have to extend the meaning of "it works" to "it works at runtime as well as in the designer". 您只需要将“它起作用”的含义扩展到“它在运行时以及在设计器中都起作用”。 So what's wrong about that? 那这怎么了?

Todays gui designers are not very restrictive and are doing a good job in "understanding" code that had been written by a human. 如今,gui设计人员的约束不是很严格,并且在“理解”人类编写的代码方面做得很好。


There is also real generated code around, for instance code generated by some xml specification, resources etc. This code is generated when building, so when it had been changed, these changes are undone whenever the application is built. 周围也有实际生成的代码,例如由某些xml规范生成的代码,资源等。此代码在构建时生成,因此,在更改它时,只要构建应用程序,这些更改都将被撤消。

Designers are not real code generators of this kind. 设计人员并不是真正的这种代码生成器。 They are a kind of "coding helpers", helping the developer to write code faster then by typing it in. But it should actually be possible to write the same kind of code manually although limiting one self to the designers capabilities is a reasonable maintainability decision. 它们是一种“编码助手”,可以帮助开发人员通过键入代码来更快地编写代码。但是实际上应该可以手动编写相同类型的代码,尽管将自己限制在设计人员的能力范围内是一个合理的可维护性决策。 。

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

相关问题 Visual Studio 2008警告关于Designer生成的代码的更改 - Visual Studio 2008 Warning About Changes to Designer-Generated Code 您如何在Visual Studio中查看c#控件的自动生成的设计器代码? - How do you view the auto generated designer code for c# controls in Visual Studio? 修改Visual Studio的表单设计器代码生成器 - Modify Visual Studio's form designer code generator 如何更改Xamarin Studio中GUI设计器生成的字段的访问级别 - How can I change access level of fields generated by GUI designer in Xamarin Studio 如何在Visual Studio中生成项目的设置设计器文件 - How to generate a project's settings designer file on build in Visual Studio 如何告诉Visual Studio不填充设计器代码中的字段? - How can I tell Visual Studio to not populate a field in the designer code? Visual Studio在设计器文件中添加代码有多糟糕? - Visual Studio how bad is it to add code in the designer files? 当我创建一个继承自 Form 的新类时,Visual Studio 会更改设计器生成的代码 - Visual Studio changes designer generated code when I create a new class inheriting from Form 您如何在WPF中仅检查Visual Studio 2010设计器(以便不针对Blend的设计器)? - How do you check for ONLY the Visual Studio 2010 designer (so that Blend's designer is NOT targeted) in WPF? 找不到Visual Studio GTK GUI设计器/源按钮 - Can not find Visual Studio GTK GUI designer/source button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM