在寫C#時常常需要執行一小段測試用的程式碼,而不管是使用MS Visual Studio或者是SharpDevelop來寫C#,都需要以開新一個測試用專案的方式來寫,著實很麻煩。
今天看到暗黑執行緒於部落格中發佈了一個工具軟體Mini C# Lab的更新訊息,這是一個可以把C#當成Script來執行的工具,同時它也是由暗黑執行緒所主導的Open Source Project,在同一篇文章中也發現了另一個有異曲同工之妙的工具Snippet Compiler。
Mini C# Lab
Snippet Compiler
Mini C# Lab 與 Snippet Compiler 是兩個屬性相同但用處不同的工具
舉例來說,如果你的測試碼是非常小的一段測試碼,就像它預設的範例碼:
using System;
using System.IO;
using System.Threading;
public class CSharpLab
{
public static void Test()
{
for (int i = 0; i < 20; i++)
{
Thread.Sleep(500);
Console.Write("Hello, World! ");
}
Console.WriteLine("\nDone!");
}
}
這個時候應該使用Mini C#來測試,因為它是以直接擷取的Output的方式來做測試,簡單方便;而Snippet Compiler卻會跑出一個Console視窗。
但如果你的測試碼是比較完整的一段,通常可能是網路上看到的範例,像它預設的範例碼:
using System;
using System.Collections.Generic;
public class MyClass
{
public static void RunSnippet()
{
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
這時候就適合使用Snippet Compiler囉,因為Mini C#要求Script裡要有個CSharpLab的類別,並且包含一個Test的方法成員,所以並不適合完整一些的測試程式碼。
總之視情況選擇對的工具,就能加快效率。
相關連結:






