I stumbled upon the following code in Juval Lowy's "Programming .NET components" on pages 95-96. The following is pretty much as he shows it, with the addition of a couple of MessageBox calls:
The above code runs fine... but this is exactly what I'm questioning...
The point that Lowy was making is that while this code is run fine:
The following code does not compile:
The reason is that the IMyInterface interface does not itself implement IDisposable. We could correct this situation by having IMyInterface implement IDisposable:
But this would be a ludicrous requirement just so that a 'using' statement could be utilized more easily; certainly not all classes implementing IMyInterface would also need to implement IDisposable.
So the solution that Juval Lowy shows is the following:
To my surprise, it does compile and run 100% fine. But what is going on here? Calling 'obj.SomeMethod()' is really a call to 'IMyInterface.SomeMethod()' and so the 'obj' is still cast as 'IMyInterface'. And yet the using statement accepts the cast to IDisposable at the same time!
Admittedly, a 'using' statement does not actually "require" an IDisposable object; the object merely needs to have a method called "Dispose()", and reflection is done at compile time to enforce that the "Dispose()" method does indeed exist. But somehow, I don't think that this is what's going on here. It does seem to be a "simultaneous cast" of sorts...
Does anyone have any thoughts?
Code:
internal interface IMyInterface
{
void SomeMethod();
}
internal class MyClass : IMyInterface, IDisposable
{
void IDisposable.Dispose()
{
MessageBox.Show("IDisposable.Dispose() called.");
}
void IMyInterface.SomeMethod()
{
MessageBox.Show("IMyInterface.SomeMethod() called.");
}
}
public static void Run()
{
IMyInterface obj = new MyClass();
using (obj as IDisposable)
{
obj.SomeMethod();
}
}
The above code runs fine... but this is exactly what I'm questioning...
The point that Lowy was making is that while this code is run fine:
Code:
MyClass obj = new MyClass();
using (obj)
{
obj.SomeMethod();
}
The following code does not compile:
Code:
IMyInterface obj = new MyClass();
using (obj)
{
obj.SomeMethod();
}
The reason is that the IMyInterface interface does not itself implement IDisposable. We could correct this situation by having IMyInterface implement IDisposable:
Code:
internal interface IMyInterface: IDisposable
{
void SomeMethod();
}
So the solution that Juval Lowy shows is the following:
Code:
public static void Run()
{
IMyInterface obj = new MyClass();
using (obj as IDisposable)
{
obj.SomeMethod();
}
}
To my surprise, it does compile and run 100% fine. But what is going on here? Calling 'obj.SomeMethod()' is really a call to 'IMyInterface.SomeMethod()' and so the 'obj' is still cast as 'IMyInterface'. And yet the using statement accepts the cast to IDisposable at the same time!
Admittedly, a 'using' statement does not actually "require" an IDisposable object; the object merely needs to have a method called "Dispose()", and reflection is done at compile time to enforce that the "Dispose()" method does indeed exist. But somehow, I don't think that this is what's going on here. It does seem to be a "simultaneous cast" of sorts...
Does anyone have any thoughts?