给出两个接口,如这些时隐式转换为System.IDisposable:无法使用 “使用”
public interface MyInterface1 : IDisposable
{
void DoSomething();
}
public interface MyInterface2 : IDisposable
{
void DoSomethingElse();
}
...和实现类是这样的:
public class MyClass : IMyInterface1, IMyInterface2
{
public void DoSomething() { Console.WriteLine("I'm doing something..."); }
public void DoSomethingElse() { Console.WriteLine("I'm doing something else..."); }
public void Dispose() { Console.WriteLine("Bye bye!"); }
}
...我倒是假设下面的代码片段应该编译:
class Program
{
public static void Main(string[] args)
{
using (MyInterface1 myInterface = new MyClass()) {
myInterface.DoSomething();
}
}
}
...相反,我总是收到以下错误信息:
Error 1 'IMyInterface1': type used in a using statement must be implicitly convertible to 'System.IDisposable'
任何想法?谢谢。
你肯定你”是否正确输入了一切?在顶部有'MyInterface1'和'MyInterface2',但稍后有'IMyInterface1'和'IMyInterface2'。 – dasblinkenlight
@ j3d - 请仅发布_accurate_代码。在发布前验证它。 –
如上所述,我们得到*其他*编译错误,而不是你所描述的错误。但是,您在编写上述类型时,每种接口类型(本身)都可以隐式转换为'IDisposable'。 –