简体   繁体   中英

Using the #if directive in designer generated code with windows forms for conditional compilation

Background :

I have a C# Windows Forms application that contains a Windows service and an interface used to configure system settings as well as communicate with the service.

Desired Outcome :

I would like to build two versions of the solution - a client version with all the Windows service related code and form elements and a server version that contains everything.

The form contains a tabbed control, where one tab contains elements used to interface with the Windows service using sockets. All I'm really trying to achieve is that for a full build the tab containing service related elements is compiled, while for a conditional build the same tab is excluded.

Problem :

At this stage I've used #if directives around the Windows service related code. For example:

#if SERVERBUILD
     //Code relating to Windows service that I do not want to compile 
     //for a client version.
#endif

In the above example, 'SERVERBUILD' corresponds to a build configuration that I can select via the Configuration Manager (as opposed to the standard 'Release' build option).

The issue I'm having is that some of the code I've had to wrap this #if directive around lies in the WinForm.Designer.cs file in the region titled ' Windows Form Designer generated code '.

What seems to be occurring is that when I make a change to some of the form properties, this entire region of code seems to be deleted and re-generated, thereby removing the #if sections I had added.

  • Am I going about this the right way?
  • Is there a way to avoid the situation where I am losing the changes I've made in the WinForm.Designer.cs code?

I would really appreciate any advice from anyone with experience with conditional compilation and this sort of stuff.

An issue with the #if approach is that you can rapidly start falling into a situtation where you have multiple #if statements for different compiles. It very quickly becomes difficult to tell which chunk of code goes with which project.

A better solution would be to identify all common code and keep them in a separate project folder, such as common/ . Then you have the specific service code under server/ for example and all forms code under client/ .

All common code will still exist in one location and you server and clients become much more readable. You no longer have to worry about client only changes affecting the server and the opposite. Plus with proper organization of folders you can keep the what of your project (abstract concepts such as client/server) apart from the how (concrete implementations such as forms).

There isn't much you can do. the Form designer is going to generate that code, and do it in a brute force way, with no care at all about your needs or changes you have made. You shouldn't muck with autogenerated code because of this.

You will need to take a new approach and figure out how to get what you need accomplished without altering the autogen code, as that's definitely a path of pure frustration.

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent() {

Nuff said. Solve your problem by putting the code in the form constructor:

    public Form1() {
        InitializeComponent();
#if !SERVERBUILD
        panel1.Visible = false;
#endif
    }

Note that using a panel is an easy way to make all the controls that are on it invisible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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