J
Joe
Hi all,
My brain is hurting. Here's the MSDN page about extension methods:
http://msdn2.microsoft.com/en-us/library/bb383977.aspx
If you don't feel like reading through it, just trust me on this next
point.
On this page, it says this:
"An extension method with the same name and signature as an interface
or class method will never be called"
Fair enough. BUT...take a look at this code:
<code>
class Program
{
static void Main(string[] args)
{
//create a new instance of MyTestClass
MyTestClass obj = new MyTestClass();
obj.SomeMethod();//call SomeMethod...but which one?
}
}
//a test interface
public interface ITestInterface
{
void SomeMethod();
}
//a class that implements the test interface explicitly
public class MyTestClass : ITestInterface
{
void ITestInterface.SomeMethod()
{
Console.WriteLine("This is executing from inside the class
implementation of the interface method");
Console.WriteLine("This is the explicit implementation of
the interface");
Console.WriteLine("Press and key to continue");
Console.ReadLine();
}
}
//static class for extension
static class MyExtensions
{
//extension has same signature as interface method
public static void SomeMethod(this ITestInterface testObject)
{
Console.WriteLine("This is executing from the interface
extension...with the same");
Console.WriteLine("method signature as the interface
method! Oh no!");
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
</code>
When you compile and run it, you'll notice that the extension hides
the explicit implementation of the interface in MyTestClass, and the
extension takes precedence over the. This only happens when the
interface is implemented explicitly (i.e. ITestInferface.SomeMethod,
not SomeMethod). So does an explicit interface implementation not
count as 'having the same signature' as the interface method?
What am I missing?
~Joe
My brain is hurting. Here's the MSDN page about extension methods:
http://msdn2.microsoft.com/en-us/library/bb383977.aspx
If you don't feel like reading through it, just trust me on this next
point.
On this page, it says this:
"An extension method with the same name and signature as an interface
or class method will never be called"
Fair enough. BUT...take a look at this code:
<code>
class Program
{
static void Main(string[] args)
{
//create a new instance of MyTestClass
MyTestClass obj = new MyTestClass();
obj.SomeMethod();//call SomeMethod...but which one?
}
}
//a test interface
public interface ITestInterface
{
void SomeMethod();
}
//a class that implements the test interface explicitly
public class MyTestClass : ITestInterface
{
void ITestInterface.SomeMethod()
{
Console.WriteLine("This is executing from inside the class
implementation of the interface method");
Console.WriteLine("This is the explicit implementation of
the interface");
Console.WriteLine("Press and key to continue");
Console.ReadLine();
}
}
//static class for extension
static class MyExtensions
{
//extension has same signature as interface method
public static void SomeMethod(this ITestInterface testObject)
{
Console.WriteLine("This is executing from the interface
extension...with the same");
Console.WriteLine("method signature as the interface
method! Oh no!");
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
</code>
When you compile and run it, you'll notice that the extension hides
the explicit implementation of the interface in MyTestClass, and the
extension takes precedence over the. This only happens when the
interface is implemented explicitly (i.e. ITestInferface.SomeMethod,
not SomeMethod). So does an explicit interface implementation not
count as 'having the same signature' as the interface method?
What am I missing?
~Joe