代码之家  ›  专栏  ›  技术社区  ›  Matt Mitchell

如何为一般类型的类编写C扩展方法

  •  8
  • Matt Mitchell  · 技术社区  · 16 年前

    希望这是一个简单的例子。

    我想向System.Web.MVC.ViewPage<t>类添加扩展方法。

    这个扩展方法应该怎么看?

    我的第一个直觉是这样的:

    namespace System.Web.Mvc
    {
        public static class ViewPageExtensions
        {
            public static string GetDefaultPageTitle(this ViewPage<Type> v)
            {
                return "";
            }
        }
    }
    

    解决方案

    一般的解决办法是 this answer .

    扩展system.web.mvc.viewpage类的具体解决方案是 my answer 下面,从 general solution .

    不同之处在于,在特定情况下,需要同时使用泛型方法声明和语句来强制将泛型类型作为引用类型。

    7 回复  |  直到 12 年前
        1
  •  16
  •   David Thibault    16 年前

    namespace System.Web.Mvc
    {
        public static class ViewPageExtensions
        {
            public static string GetDefaultPageTitle<T>(this ViewPage<T> v)
            {
                return "";
            }
        }
    }
    
        2
  •  5
  •   Matt Mitchell    16 年前

    this page

    namespace System.Web.Mvc
    {
        public static class ViewPageExtensions
        {
            public static string GetDefaultPageTitle<T>(this ViewPage<T> v) 
              where T : class
            {
                return "";
            }
        }
    }
    
        3
  •  3
  •   Corey Ross    16 年前

    namespace System.Web.Mvc
    {
        public static class ViewPageExtensions
        {
            public static string GetDefaultPageTitle<Type>(this ViewPage<Type> v)
            {
                return "";
            }
        }
    }
    

        4
  •  2
  •   chadmyers    16 年前
    namespace System.Web.Mvc
    {
        public static class ViewPageExtensions
        {
            public static string GetDefaultPageTitle<T>(this ViewPage<T> view)
                where T : class
            {
                return "";
            }
        }
    }
    

        5
  •  1
  •   Eric Schoonover thSoft    16 年前

    Glenn Block ForEach IEnumerable<T>

    blog post

    public static class IEnumerableUtils
    {
        public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
        {
            foreach(T item in collection)
                action(item);
        }
    }
    
        6
  •  1
  •   Tim Jarvis    16 年前

    public static string GetDefaultPageTitle(this ViewPage<YourSpecificType> v)
    {
      ...
    }
    

        7
  •  1
  •   Tod Thomson    12 年前

    public static class WebViewPageExtensions
    {
        public static string GetFormActionUrl(this WebViewPage view)
        {
            return string.Format("/{0}/{1}/{2}", view.GetController(), view.GetAction(), view.GetId());
        }
    
        public static string GetController(this WebViewPage view)
        {
            return Get(view, "controller");
        }
    
        public static string GetAction(this WebViewPage view)
        {
            return Get(view, "action");
        }
    
        public static string GetId(this WebViewPage view)
        {
            return Get(view, "id");
        }
    
        private static string Get(WebViewPage view, string key)
        {
            return view.ViewContext.Controller.ValueProvider.GetValue(key).RawValue.ToString();
        }
    }