<p>This should hopefully be a simple one.</p>

<p>I would like to add an extension method to the System.Web.Mvc.ViewPage&lt; T > class.</p>

<p>How should this extension method look?</p>

<p>My first intuitive thought is something like this:</p>

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

<p><strong>Solution</strong></p>

<p>The general solution is <a href="https://stackoverflow.com/questions/68750/how-do-you-write-a-c-extension-method-for-a-generically-typed-class#68772">this answer</a>.</p>

<p>The specific solution to extending the System.Web.Mvc.ViewPage class is <a href="https://stackoverflow.com/questions/68750/how-do-you-write-a-c-extension-method-for-a-generically-typed-class#68802">my answer</a> below, which started from the <a href="https://stackoverflow.com/questions/68750/how-do-you-write-a-c-extension-method-for-a-generically-typed-class#68772">general solution</a>.</p>

<p>The difference is in the specific case you need both a generically typed method declaration AND a statement to enforce the generic type as a reference type.</p>

[{'answer_id': 68772, 'author': 'David Thibault', 'author_id': 5903, 'author_profile': 'https://Stackoverflow.com/users/5903', 'pm_score': 5, 'selected': True, 'text': '<p>I don\'t have VS installed on my current machine, but I think the syntax would be:</p>\n\n<pre><code>namespace System.Web.Mvc\n{\n    public static class ViewPageExtensions\n    {\n        public static string GetDefaultPageTitle&lt;T&gt;(this ViewPage&lt;T&gt; v)\n        {\n            return "";\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 68779, 'author': 'Corey Ross', 'author_id': 5927, 'author_profile': 'https://Stackoverflow.com/users/5927', 'pm_score': 2, 'selected': False, 'text': '<p>It just needs the generic type specifier on the function:</p>\n\n<pre><code>namespace System.Web.Mvc\n{\n    public static class ViewPageExtensions\n    {\n        public static string GetDefaultPageTitle&lt;Type&gt;(this ViewPage&lt;Type&gt; v)\n        {\n            return "";\n        }\n    }\n}\n</code></pre>\n\n<p>Edit: Just missed it by seconds!</p>\n'}, {'answer_id': 68802, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 3, 'selected': False, 'text': '<p>Thanks leddt.\nDoing that yielded the error:</p>\n\n<blockquote>\n  <p>The type \'TModel\' must be a reference\n  type in order to use it as parameter\n  \'TModel\' in the generic type or method</p>\n</blockquote>\n\n<p>which pointed me to <a href="http://forums.asp.net/p/1311022/2581563.aspx" rel="noreferrer">this page</a>, which yielded this solution:</p>\n\n<pre><code>namespace System.Web.Mvc\n{\n    public static class ViewPageExtensions\n    {\n        public static string GetDefaultPageTitle&lt;T&gt;(this ViewPage&lt;T&gt; v) \n          where T : class\n        {\n            return "";\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 68826, 'author': 'Tim Jarvis', 'author_id': 10387, 'author_profile': 'https://Stackoverflow.com/users/10387', 'pm_score': 1, 'selected': False, 'text': '<p>If you want the extension to only be available for the specified type\nyou simply just need to specify the actual type you will be handling</p>\n\n<p>something like...</p>\n\n<pre><code>public static string GetDefaultPageTitle(this ViewPage&lt;YourSpecificType&gt; v)\n{\n  ...\n}\n</code></pre>\n\n<p>Note intellisense will then only display the extension method when you declare your (in this case) ViewPage with the matching type.</p>\n\n<p>Also, best not to use the System.Web.Mvc namespace, I know its convenient to not have to include your namespace in the usings section, but its far more maintainable if you create your own extensions namespace for your extension functions.</p>\n'}, {'answer_id': 68864, 'author': 'Eric Schoonover', 'author_id': 3957, 'author_profile': 'https://Stackoverflow.com/users/3957', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://codebetter.com/blogs/glenn.block/archive/2008/08/19/foreach-a-simple-but-very-useful-extension-method.aspx" rel="nofollow noreferrer">Glenn Block</a> has a good example of implementing a <code>ForEach</code> extension method to <code>IEnumerable&lt;T&gt;</code>.</p>\n\n<p>From his <a href="http://codebetter.com/blogs/glenn.block/archive/2008/08/19/foreach-a-simple-but-very-useful-extension-method.aspx" rel="nofollow noreferrer">blog post</a>:</p>\n\n<pre><code>public static class IEnumerableUtils\n{\n    public static void ForEach&lt;T&gt;(this IEnumerable&lt;T&gt; collection, Action&lt;T&gt; action)\n    {\n        foreach(T item in collection)\n            action(item);\n    }\n}\n</code></pre>\n'}, {'answer_id': 68935, 'author': 'chadmyers', 'author_id': 10862, 'author_profile': 'https://Stackoverflow.com/users/10862', 'pm_score': 2, 'selected': False, 'text': '<pre><code>namespace System.Web.Mvc\n{\n    public static class ViewPageExtensions\n    {\n        public static string GetDefaultPageTitle&lt;T&gt;(this ViewPage&lt;T&gt; view)\n            where T : class\n        {\n            return "";\n        }\n    }\n}\n</code></pre>\n\n<p>You may also need/wish to add the "new()" qualifier to the generic type (i.e. "where T : class, new()" to enforce that T is both a reference type (class) and has a parameterless constructor.</p>\n'}, {'answer_id': 11771468, 'author': 'Tod Thomson', 'author_id': 372666, 'author_profile': 'https://Stackoverflow.com/users/372666', 'pm_score': 1, 'selected': False, 'text': '<p>Here\'s an example for Razor views:</p>\n\n<pre><code>public static class WebViewPageExtensions\n{\n    public static string GetFormActionUrl(this WebViewPage view)\n    {\n        return string.Format("/{0}/{1}/{2}", view.GetController(), view.GetAction(), view.GetId());\n    }\n\n    public static string GetController(this WebViewPage view)\n    {\n        return Get(view, "controller");\n    }\n\n    public static string GetAction(this WebViewPage view)\n    {\n        return Get(view, "action");\n    }\n\n    public static string GetId(this WebViewPage view)\n    {\n        return Get(view, "id");\n    }\n\n    private static string Get(WebViewPage view, string key)\n    {\n        return view.ViewContext.Controller.ValueProvider.GetValue(key).RawValue.ToString();\n    }\n}\n</code></pre>\n\n<p>You really don\'t need to use the Generic  version as the generic one extends the non-generic one so just put it in the non-generic base class and you\'re done :)</p>\n'}]
['https://Stackoverflow.com/questions/68750', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/364/']