How to define extension methods for generic class?

Try that. Notice the T immediately after the Add.

Up vote 6 down vote favorite share g+ share fb share tw.

I am having a generic interface as: public IRepository { void Add(T entity); } and a class as: public class Repository:IRepository { 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 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. Please guide the appropriate way.

C# methods extension generics link|improve this question edited Sep 20 '10 at 7:28Vivien Barousse7,204621 asked Sep 20 '10 at 7:25Samreen423 60% accept rate.

Public static void Add(this IRepository dataAccessRepository, T entity, string additionalValue) Try that. Notice the immediately after the Add.

But in this case whenever I would call this extension method I would be passing this Type. Right now I do like this: IRepository employeRepository = new Repository (); employeeRepository. Add(entity); But in the case you suggested I'd be passing the type to The extension method too as: employeeRepository(entity,"someValue"); – Samreen Sep 20 '10 at 7:29 Did you try it?

C# and . Net will figure out the type based on the IRepository's type – Rohith Sep 20 '10 at 7:36 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 Sep 20 '10 at 7:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions