Inheriting an interface with IDisposable?

I have the following inheritance hierarchy:

public interface IRepository<T> : IDisposable { void Add(T model); void Update(T model); int GetCount(); T GetById(int id); ICollection<T> GetAll(); } public interface IAddressRepository : IRepository<Address> { } 

And this code:

 var adrs = new Address[]{ new Address{Name="Office"} }; using (IAddressRepository adrr = new AddressRepository()) foreach (var a in adrs) adrr.Add(a); 

However, this code does not compile. This gives me an error message:

 Error 43 'Interfaces.IAddressRepository': type used in a using statement must be implicitly convertible to 'System.IDisposable' 

However, the parent IAddressRepository element inherits from IDisposable .

What's going on here? How to compile code?

+7
source share
2 answers

I assume that you are mistaken: you did not recompile the assembly containing the IRepository<T> interface, because you inherited it from IDisposable , or referenced its incorrect copy, or referenced some other IAddressRepository .

Try doing "Clear", then "Restore All" and check the paths in your links. If the projects are in the same solution, make sure that you refer to the project containing the IRepository<T> / IAddressRepository , and not the DLL.

Also make sure AddressRepository really implements IAddressRepository . It could just be a bad error message.

EDIT: So the resolution seems to be that the assembly containing the parent class AddressRepository did not compile. This made the debugger complain about the AddressRepository not implementing IDisposable , and not the (more reasonable) "unreachable due to the level of protection" error compiling the class itself. I assume that you also had this error, but first turned to it.

+7
source

Works for me:

 using System; public class Address {} public interface IRepository<T> : IDisposable { void Add(T model); void Update(T model); } public interface IAddressRepository : IRepository<Address> { } class Program { public static void Main() { using (var repo = GetRepository()) { } } private static IAddressRepository GetRepository() { // TODO: Implement :) return null; } } 

I suspect you may have two IAddressRepository interfaces. Are you sure it has Interfaces.IAddressRepository that extends IRepository<T> , and that extends IDisposable ?

+2
source

All Articles