Conditional compilation in C#

引用此網頁

Conditional
compilation in C#

       Programmers need to
debug, which sometimes requires identification of points in your program
where a programmer would like to insert code that would help him/her to
debug his/her code efficiently. A simple example might be inserting a
Console.Writeline() call that prints out values or indicates completion
(successful or unsuccessful) of the executed part. However, these lines
can clutter up the code structure and also needs removal of the
debugging code for the release of the entire software.

            
This overhead is taken care of by specialized methods in C# that
help the programmer debug the code without the need to clean up his/her
debugging code for the release phase. These methods which are used for
debugging are called Conditional methods.
The compiler identifies these marked methods and never includes them in
the release build.

            
Well, that’s good enough. But, how do I make a method
conditional? .NET provides an attribute System.Diagnostics.ConditionalAttribute
(alias
Conditional) to achieve this. Let’s
look at some code now.

  Defining the conditional
method

 public class MyTracer
{
    [Conditional(“DEBUG”)]
    public static
void LogThisMessage(string myMessage, int
severity)
    {
        //  Write message to screen
or a file or …
        Console.WriteLine(” DEBUG MESSAGE : “ + myMessage
+” SEVERITY LEVEL : “+severity);
    }
}

 

The Conditional attribute has been applied to the LogThisMessage() method with the DEBUG conditional compilation symbol. This signals the
compiler that the method should be ignored if the conditional symbol DEBUG is not specified.

 

Using the conditional method

 With the Solution
Configurations
set to Debug mode if you execute the following
code, you would happily see the output window shown below.

 

public static void
Main(string[]
args)
{
    //  Some error occured in my
code
    //  Log the message
    MyTracer.LogThisMessage(“The error 1221 has occured.”,
5);
}

 DEBUG on

 

 

Now lets see what happens
to our conditional code in the Release mode. To enable Debug or Release
mode you have to look for the Solution Configurations selector in your
Visual Studio IDE which is shown below.

 

 

Change the mode to “Release” and you would see that the conditional code
now has disappeared.

 

 

 

We used the predefined DEBUG compilation symbol in our code. You can have
your own defined symbols and use them for conditional compilation. You
can define custom compilation symbols in Project Properties -> Build
tab -> General. Checkboxes have been provided to enable or disable
the DEBUG and TRACE
symbols.

 

 

 

.NET also provides two
classes that provide similar functionality:

  • System.Diagnostics.Debug
  • System.Diagnostics.Trace

 These classes
contain methods that can also be used for debugging if you do not need
more specific custom methods.

都是「ASP.NET Development Server」惹的禍

最近在用VS2008開發ASP.NET的程式,前兩天在我努力寫完了一堆邏輯程式碼後,想要Debug 一下看邏輯是否正確時,老天爺開了我一個很大的玩笑,按下F5後,很開心的等畫面出現,結果畫面卻是無法顯示網頁,我整個傻掉,想說別在這時候玩我呀!

當遇到困難時,我都是求救於骨狗大神,發現很多人都有同樣問題,不過找不到一個好的方法,於是,我秉持著實驗精神,把VS2008跟IIS重灌了,結果……

一樣不能跑

而且更慘的是IIS不能跑ASP.NET,千萬記得以後安裝要先IIS再灌.NET,要不然問題真的很多。

要不然就跟我一樣,又重灌了IIS,記得檢查IIS裡面的細項,你所需要的項目是否已經勾選。

這時候雖然直接RUN站台是可以work的,不過用VS2008還是一樣無法顯示網頁,這時候我心血來潮的想到一個方法,就是先按F5後,讓網頁跑出來..接下來

我把 「localhost」改成「127.0.0.1」竟然可以跑了,而且還可以debug

這是怎樣啦!嗚嗚!算了!先救火比較重要,之後再想辦法解決。

ToolkitScriptManager's Error Message in AJAX Toolkit

在開發神啊!系統時,有一個步驟因為要等待某個外部程式執行完畢才可以go through,然後當程式在運作時,我同時會在UI上面擺一個BUSY WAITING的動畫,但是每次都大約兩分鐘不到就停止了。

被這問題困擾多天的我,昨天終於發現是ToolkitScriptManager這元件在作怪,有一個屬性是設定timeout的時間,Default是90s,然怪每次都固定的time slice就停止。另外一點是我都是用FireFox在做測試,昨天覺得每次用FireFox跑「神啊!」都發現他的記憶體越吃越多,但是我初步猜測是廢鐵的Memory Leak很嚴重,因為他是用C++寫的,不過還沒去證實他。

因為記憶體暴漲,因而決定測試的時候改用IE,至少關閉的時候,應該會全部release。就是因為改用IE測試,才發現這個錯誤訊息:

WebForms.PageRequestManagerTimeoutException

因此知道原來是TimeOut,哈!Web經驗還是要多多加強。

最後的解決方法就是把AsyncPostBackTimeout設為0,意思就是無限制。

終於可以在往下一步了。

The Will Will Web 記載著 Will 在網路世界的學習心得與技術分享 You searched for 'vs2008 瀏覽器 設定' Here are some results

引用於此

有許多網頁設計師還是習慣用 Table 進行網頁版面配置,但各位應該知道在 IE 下, 只要 HTML 的 <img>
標籤與表格儲存格 </td> 之間只要有一個空白字元出現,就會導致網頁版面跑掉「一點點」,有時後就是這「一點點」讓套版人員抓狂。

我們在 Visual Studio 中若使用「格式化文件(Format Document)」功能進行 HTML 排版,預設的情況下,就會導致網站破版,以致於我們很少在 HTML 格式下進行排版作業。

如下圖例是尚未格式化的表格 HTML 原始碼:

 尚未格式化的表格 HTML 原始碼

經過 Visual Studio 排版(Ctrl + E + D)後變成如下圖這樣,雖然比較漂亮,但在 IE 下看卻會破版:

經過 Visual Studio 排版後的HTML

我今天就教各位修改 Visual Studio 中的設定,讓你以後再自動格式化文件時不會再破壞版面了。

以下是設定的步驟:

1. 工具 –> 選項 –> Text Editor –> HTML –> Format –> Tag Specific Options

工具 –> 選項 –> Text Editor –> HTML –> Format –> Tag Specific Options

2. Client HTML Tags –> New Tag… 並新增 img 標籤(Tag)

Client HTML Tags –> New Tag...

New Tag -> img

3. 把 Line breaks 切換到 None 即可,你也可以看到 Preview 欄位的預覽效果。

Client HTML Tags –> img  –> Line breaks –> None

4. 接著修改 td 標籤的設定,並切換到 Before, after opening, and after closing 即可。

Client HTML Tags –> td  –> Line breaks –> Before, after opening, and after closing

設定完成後,就可以直接試著格式化文件看看。如下圖就是修改選項後進行格式化的結果:

修改選項後進行格式化的結果

以後就可以放心格式化文件了。

特別提醒

  • 對於已經進入 Production 階段的專案,切勿再使用「格式化文件」功能,以免造成 Code Reviewer 的困擾!