这是一个人为制造的例子:
public static class MyExtensions
{
public static void MyMethod( this MyInterface obj, string txt )
{
}
}
interface MyInterface {}
public MyDerived : MyInterface
{
void DoStuff()
{
MyMethod( "test" ); // fails; compiler can't find MyMethod?
}
}
在我上面的例子中,我试图从我的派生类中调用分配给接口的扩展方法。编译器在这里失败,并表示MyMethod不存在于当前上下文中。我在CS文件中拥有所有适当的using语句,所以我不知道发生了什么。
this
。这只是一个小小的不便,无法与你从中获得的所有好处相比 :-) - Darin Dimitrov