Visual Studio Source Code Source Code

I am refactoring a program containing many forms created dynamically from runtime information.

To reduce the complexity of the system, I decided to write separate code files for each of these forms. Since there are many forms, I’m thinking of a way to automate the process of creating source files for forms from data collected at run time.

eg. if I have a run-time instance of a form called EditPeople, I want to create the source code of EditPeople.designer.cs so that I can then edit the form in the Windows form designer.

Do you know if there is some kind of infrastructure or tool that can simplify this task?

+6
c # visual-studio-2010
source share
3 answers

I just saw this question, for a future reference, you could try something like this:

public static List<Control> GetAllControls(IList ctrls) { List<Control> FormCtrls = new List<Control>(); foreach (Control ctl in ctrls) { FormCtrls .Add(ctl); List<Control> SubCtrls = GetAllControls(ctl.Controls); FormCtrls .AddRange(SubCtrls); } return FormCtrls; } 

You can use this function as follows:

 List<Control> ReturnedCtrls = GetAllControls(MyForm.Controls); 

When you have a list of all the controls, you can do something like this:

 foreach(Control ctrl in ReturnedCtrls) { // Generate Designer Source using ctrl properties ctrl.Left ctrl.Top // etc... } 
+1
source share

If the intention is to recreate the form code, I think using the .net reflector http://www.red-gate.com/products/reflector/ along with my addon FileDisassembler denisbauer.com/NETTools/FileDisassembler.aspx(ya I can’t post more than 1 hyperlink :)) will help you in decompiling a complete project in a short time.

Don't you think?

0
source share

2 CodeCanvas - No way. Reflector gives you a class, and the task is to serialize all instances of all the given classes. For example, the class contains the code:

 for(int i=0; i<2;i++){ Controls.Add(new Button()); } 
Task

is to make * .Designer.cs with the following code:

 //... InitializeComponent() { //... button1 = new Button(); button2 = new Button(); button3 = new Button(); //... this.Controls.Add(button1); this.Controls.Add(button2); this.Controls.Add(button3); } //... 

and this is definitely not what the reflector gives you.

Serializing CodeDom can do the trick, but it acts by its own rules and is unlikely to give a full snapshot of the form.

0
source share

All Articles