我是否一直把我的静态方法放在课堂里?

时间:2013-05-29 22:29:37

标签: c# class nested extension-methods

我有这样的事情:

public class Download
    {
        public int Id = 0; //RecordId
        public string Title = null;
        public StateData States = new StateData();
        public List<string> Genres = new List<string>();
        public DownloadType DownloadType = DownloadType.None;
        public string UrlTag = null;
        public int MasterId = 0; // MasterId
        public string PosterLink = null;
        public decimal Rating = 0;
        public DateTime Released = new DateTime();
        public List<string> Tags = new List<string>();
    }

public class Game : Download
    {
        public string Genre = null;
        public string Developer = null;
        public string Publisher = null;
    }

    public class PSP : Game
    {
    }

    public class PSVita : Game
    {
    }

现在,我可以像这样添加必要的扩展名:

namespace VitaHtmlExtension
{
    public static class PSVitaExtentions
    {
        public static string GetViewUrl(this PSVita Game, Page Page)
        {
            return null;
        }
        public static string ToHtml(this PSVita Game, Page Page, string WrapperClass, string TitleClass, string ImageAndDetailWrapper, string ImageHolderClass, string InfoHolderClass, string InfoWrapperClass, string InfoFieldClass, bool IsClearFixOn)
        {
            return null;
        }

        public string ToMiniHtml(this PSVita Game, Page Page, string WrapperClass, string TitleClass, string ImageHolderClass, string TitleAndInfoWrapper, string InfoHolderClass, string InfoWrapperClass, string InfoFieldClass, bool IsClearFixOn)
        {
            return null;
        }
        /// <summary>
        /// Returns Html Code for a Download to print in the Index Results.
        /// </summary>
        /// <param name="Page">Reference to Page.</param>
        /// <returns></returns>
        public string ToResultThumb(this PSVita Game , Page Page)
        {
            string Html = Game.ToHtml(Page, "Result_Thumb", "ResultTitle", "ResultDetails", "ResultThumbImage", "ResultInfoDiv", "Infos", "InfoField", true);
            return Html;
        }
        /// <summary>
        /// Returns HTML Code for SideBar View of Download.
        /// </summary>
        /// <param name="Page"></param>
        /// <returns></returns>
        public string ToSideThumb(this PSVita Game, Page Page)
        {
            return Game.ToMiniHtml(Page, "SideBoxThumb", "SideThumbTitle", "SideThumbImage", "SideThumbInfoDiv", "SideThumbInfo", "SideBoxThumbInfoSpan", "InfoField", true);
        }
        /// <summary>
        /// Returns HTML Code for More Item View of Download.
        /// </summary>
        /// <param name="Page"></param>
        /// <returns></returns>
        public string ToMoreThumb(this PSVita Game, Page Page)
        {
            return Game.ToHtml(Page, "MoreItem", "Title", "WrapClass", "Image", "Infos", "WrapClass", "InfoField", false);
        }

        //Supposed to be static but well I can't.
        public static string GetParamUrl(this PSVita Game,string Param, string Value, Page Page)
        {
            return null;
        }

        public static string GetIndexPageUrl(this PSVita Game, Page Page)
        {
            return null;
        }
    }
}

现在我需要编写一些特定于上下文的静态方法,比如当我在WinForms中使用对象时,我不需要与html相关的静态方法。有没有办法描述静态方法,除了将它们嵌套在对象类中之外,就像我可以使用Extension方法一样?

2 个答案:

答案 0 :(得分:0)

  

现在我需要编写一些特定于上下文的静态方法,比如当我在WinForms中使用对象时,我不需要与html相关的静态方法。有没有办法描述静态方法,除了将它们嵌套在对象类中之外,就像我可以使用Extension方法一样?

您可以将它们放在自己的班级中,与他们所扮演的“对象”分开。这实际上是扩展方法的全部 - 它是一个单独的类中的静态方法。

答案 1 :(得分:0)

您可以将扩展方法放在任意数量的静态类中,您的PSVitaExtentions类没有什么特别之处。只要你已经引入了它们的命名空间,扩展方法就是可调用的,所以@griegs说将第二组扩展方法放在不同的命名空间中可能是明智的。