Posts Tagged ‘c#’

ListTransformer

Friday, November 23rd, 2007

呢排寫緊好多比較 Generic 既 class, 希望可以 improve reusable, 所以係 Code Project 度問左呢個 class, 主要係將一個 object List 變左一個 generic List, 咁就可以方便 d.

public class ListTranformer
{
    public static List<D> Transform<D>(System.Collections.IList list)
    {
        List lst = new List();
        foreach (D element in list)
        {
            lst.Add(element);
        }
        return lst;
    }
}

Implement Timeout in C#

Wednesday, October 24th, 2007

最近要幫個 hardware 寫返個 timeout, 主要既目的唔想佢 hold 咁耐, 好似 hang 左咁, 所以就係網上搵左呢個較好既方法.
其實主要係用左 Delegate, define 個 delegate 同target function 同樣 return type 同 parameter, 之後將果個 function 放係 delegate 個 constructor 果度, private 左之前果個 function, 將外部既 code call 新既 method (Execute), Set 埋個 Timeout 可以自己做個 TimeoutException~~

delegate int WorkDelegate(string arg);

public int Execute(string arg)
{
        WorkDelegate d = new WorkDelegate(TargetFunction);
        IAsyncResult res = d.BeginInvoke(arg, null, null);

        if (res.IsCompleted == false)
        {
                //Timeout when execute more than 10 sec
                res.AsyncWaitHandle.WaitOne(10000, false);
                if (res.IsCompleted == false)
                {
                        throw new TimeoutException("Timeout");
                }
        }
        return d.EndInvoke((AsyncResult)res);
}

Test-driven development

Tuesday, August 14th, 2007

今日, 試左用 [http://nunit.org nunit] 黎寫 C# 既 Test Case, 感覺都 ok, 我諗我應該會開始寫呢方面既野多D.

因為呢排幫某 D project 轉做 [http://asp.net ASP.net] 2.0 同 refactoring, 所以就用緊 VS 2005 Express Edition, 而因為係 Express, 當我要 Debug ASP 果時, 行到有關 Dll 既位我就行唔到, 因為 Dll 係 C# Express, 而 ASP.net 係 Web Developer Express, 所以而家要用 Test Case 黎幫手做 Test!!~~

C# Refactory

Tuesday, August 14th, 2007

[http://www.xtreme-simplicity.net/CSharpRefactory.html C# Refactory] 係一個幾多 [http://www.refactoring.com/ refactoring] function 既 tool, 而且仲係  [http://www.xtreme-simplicity.net/PlaceOrder.htm free], 不過佢好似只可以 support 到 VS 2003, 當我用 VC# 2005 Express 果時就無得用了 >,<