Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you write a C# Extension Method for a Generically Typed Class

This should hopefully be a simple one.

I would like to add an extension method to the System.Web.Mvc.ViewPage< T > class.

How should this extension method look?

My first intuitive thought is something like this:

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

Solution

The general solution is this answer.

The specific solution to extending the System.Web.Mvc.ViewPage class is my answer below, which started from the general solution.

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.

like image 399
Matt Mitchell Avatar asked Sep 16 '08 02:09

Matt Mitchell


People also ask

What do you write C code in?

A C program source code can be written in any text editor; however the file should be saved with . c extension.

How do you write a conditional statement in C?

C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.


4 Answers

Thanks leddt. Doing that yielded the error:

The type 'TModel' must be a reference type in order to use it as parameter 'TModel' in the generic type or method

which pointed me to this page, which yielded this solution:

namespace System.Web.Mvc
{
    public static class ViewPageExtensions
    {
        public static string GetDefaultPageTitle<T>(this ViewPage<T> v) 
          where T : class
        {
            return "";
        }
    }
}
like image 65
Matt Mitchell Avatar answered Sep 16 '22 19:09

Matt Mitchell


I don't have VS installed on my current machine, but I think the syntax would be:

namespace System.Web.Mvc
{
    public static class ViewPageExtensions
    {
        public static string GetDefaultPageTitle<T>(this ViewPage<T> v)
        {
            return "";
        }
    }
}
like image 20
David Thibault Avatar answered Sep 18 '22 19:09

David Thibault


It just needs the generic type specifier on the function:

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

Edit: Just missed it by seconds!

like image 25
Corey Ross Avatar answered Sep 17 '22 19:09

Corey Ross


namespace System.Web.Mvc
{
    public static class ViewPageExtensions
    {
        public static string GetDefaultPageTitle<T>(this ViewPage<T> view)
            where T : class
        {
            return "";
        }
    }
}

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.

like image 30
chadmyers Avatar answered Sep 16 '22 19:09

chadmyers