可能重複:
C# - Is there a better alternative than this to ‘switch on type’?如何在「可能」類型的對象之間切換?
我公司的遺留代碼有什麼如下
public override Uri GetUri(object obj)
{
if ((obj is Entry) || (obj is EntryComment))
{
//
}
if (obj is Blog)
{
//
}
// if obj is blah blah blah
}
這種方法只是醜陋。我想重構它,但我不知道一種技術來遍歷obj可能的「可能」類型。
我該如何重構這個?
謝謝。
我添加了region指令來演示代碼有多醜:-s – Vimvq1987 2011-05-10 08:36:07
您可以將if語句塊內的代碼移入類型本身嗎?那樣你的方法就是:'return obj.Uri;'。 – 2011-05-10 08:36:50
@Lasse V. Karlsen,+1爲您的評論。 – 2011-05-10 08:41:01