views:

85

answers:

1

Hi All,

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.

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

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

Rohith
But in this case whenever I would call this extension method I would be passing this Type. Right now I do like this: IRepository< Employee > employeRepository = new Repository < Employee >();employeeRepository.Add(entity);But in the case you suggested I'd be passing the type to The extension method too as: employeeRepository< Employee >(entity,"someValue");
Samreen
Did you try it? C# and .Net will figure out the type based on the IRepository's type
Rohith
yes I tried and it worked. Thanks a lot Rohith for your help. I was exactly doing the same but I was also passing the Type when I was calling the method. I tried it without passing the Type 'T' and .Net figured out:) Thanks a lot
Samreen