Changing autogenerated code in a C# Windows Forms Application project

纵饮孤独 提交于 2019-11-28 12:53:45

Why do you want to change the auto-generated files?
In case of forms you should not change .Designer.cs or the .resx files because they will be generated everytime you change the form so whatever changes you do will be overwritten next time the code is regenerated.
If you want to change something in Designer.cs, you can do that change in the original cs file because the class is marked as partial which means that the code from the files Form1.cs and Form1.Designer.cs is combined to generate the final code.
If you want to add your resources add a resource file and leave the generated .resx file intact. For AssemblyInfo.cs you can change the general information of the assembly.

Long story short just leave the generated files intact.

The auto-generated files:

  • AssemblyInfo.cs : the attributes inside it are set by Project + Properties, Application Tab, Assembly Information button. Editing the file directly is okay, the IDE doesn't easily get confused by it.

  • Settings.Designer.cs : auto-generated from the Settings.settings file which in turn is generated from the Settings designer. Any edits to this file will be lost quickly.

  • Resources.Designer.cs : auto-generated from the Resources.resx file which in turn is generated from the Resources designer. Any edits to this file will be lost quickly.

  • Form.Designer.cs : auto-generated by the form designer. Editing this file is possible, there is no independent file that stores the form layout. The design view is generated by running this code at design time and using Reflection to recover the form and control properties. And saved again when you close the designer, it re-generates the InitializeComponent method and the control variables. Getting your code changes to survive this process requires writing the code exactly the way the designer itself would. Which is fairly untrivial when you make changes beyond simple property value changes. There's also considerable risk, an unwise change can easily cause an exception. When that happens at design time, you get the infamous WSOD (White Screen Of Darn) which displays an often very cryptic exception and tells you that you cannot design your form anymore.

You can learn more about the way the designer generates code by observing the changes it makes to InitializeComponent when you modify the form in the designer. Getting this right is certainly not beginner material, you are probably have more pressing things to learn while you get up to speed on .NET programming. The designers are there to make your life easier and to minimize the risk of getting it wrong.

Most of those files are templated and you can modify their inclusion in a new project and what is contained in the files. You can even create your own templates for new project types or items. http://msdn.microsoft.com/en-us/magazine/cc188697.aspx

As far as modifying what is already there, unless you're sure that every project you're going to work on is going to need those changes I wouldn't recommend doing that, it's easier to just create a new template for a different project/file type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!