另一種選擇,是創造所有ViewPages的基類。隱藏現有的HtmlHelper財產與自己的版本:
public class MyHtmlHelper : HtmlHelper
{
// Take helper service, implement special methods
}
public class MyViewPage : ViewPage
{
protected new MyHtmlHelper HtmlHelper { get; set; }
// Wire up HtmlHelper as appropriate -- which is not going to be easy since
// the base property could be set at any time.
}
現在所有視圖頁面會使用您的特殊MyHtmlHelper所有您特製的醬汁,他們甚至都不需要是擴展方法。一種變體是讓您的MyHtmlHelper公開該服務並繼續編寫定位該幫助程序的擴展方法。
適應靜態類:
有時某個應用程序將相關功能集成到一類的靜態函數:
static class IdGenerator
{
static int _next = 0;
public static int GetNext()
{
return ++_next;
}
}
這種
衛生署,是啊。我修好了它。 – 2009-06-08 15:17:26