Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define extension methods for generic class?

I am having a generic interface as:

public IRepository< T >
{

    void Add(T entity);

}

and a class as:

public class Repository< T >:IRepository< T >
{

    void Add(T entity)
    {      //Some Implementation
    }

}

Now I want to make an extension method of the above interface. I made the following class:

public static class RepositoryExtension

{

    public static void Add(this IRepository< T > dataAccessRepository, T entity, string additionalValue)

    {
        //Some Implementation
    }

}

But I get error in the extension Add method. It doesn't recognize the Type 'T' I have passed to IRepository. I cannot event pass this Type to my Extenstion Methods class i.e RepositoryExtension< T >. Please guide the appropriate way.

like image 671
Sam P Avatar asked Sep 20 '10 07:09

Sam P


1 Answers

public static void Add<T>(this IRepository< T > dataAccessRepository, T entity, string additionalValue)

Try that. Notice the <T> immediately after the Add

like image 66
Rohith Avatar answered Sep 19 '22 23:09

Rohith