从 Idisposable 实现泛型 class 时编译时的不同行为?

Different behaviour at compile time while implementing generic class from Idisposable?

为什么在使用以下代码时行为会发生变化

public class Repository<T> : IRepository<T> where T : BaseEntity, IDisposable

public class Repository<T> : IDisposable, IRepository<T> where T : BaseEntity

如果我将实现 class 留空,在上述情况下它不希望我实现 Dispose() 方法。但是,在下面需要实现 Dispose() 方法。 下面是完整的测试代码:

public interface Itest<T> where T: testA{ }
public class testA { }
public class test2<T> : Itest<T> where T : testA,IDisposable{ } //successfully compiled
public class test3<T> : IDisposable, Itest<T> where T : testA { }//Failed compiled : need to implement Dispose()

当你有

public class Repository<T> : IRepository<T> where T : BaseEntity, IDisposable

那么T必须实施IDisposable

当你有

public class Repository<T> : IDisposable, IRepository<T> where T : BaseEntity

那么Repository必须实施IDisposable

当您想创建 test2<T> 的实例时,您应该提供一个从 testA 派生并实现 IDisposable.

的通用参数

在第一个代码示例中,T 必须实现 IDisposable。在第二个代码示例中,Repository 本身必须实现 IDisposable.