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. It gives me this error message:
Error 43
'Interfaces.IAddressRepository': type used in a using statement must be
implicitly convertible to 'System.IDisposable'
However, a parent of IAddressRepository inherits from IDisposable.
What is happening here? How do I make the code compile?
My guess is that you are making a mistake- either you have not recompiled the assembly containing the
IRepository<T>interface since you made it inherit fromIDisposable, or you are referencing the wrong copy of it, or you are referencing some otherIAddressRepository.Try doing a Clean, then a Rebuild All, and check the paths on your references. If the projects are in the same solution, make sure you are referencing the project containing
IRepository<T>/IAddressRepositoryand not the DLL.Also make sure that
AddressRepositoryactually implementsIAddressRepository. It might just be reporting the wrong error.EDIT: So the resolution seems to be that the assembly containing
AddressRepository‘s parent class was not compiling. This caused the debugger to complain aboutAddressRepositorynot implementingIDisposable, rather than the (more sensible) ” inaccessible due to its protection level” error compiling the class itself. My guess is you had that error too, but were addressing this one first.