在 C# 中从实现者调用接口扩展方法很奇怪
从实现者调用在接口上工作的扩展方法似乎需要使用 this 关键字。这看起来很奇怪。
有谁知道为什么?
有没有更简单的方法来获得接口的共享实现?
这让我很恼火,因为我正在遭受多重继承/混合退出的痛苦。
玩具示例:
public interface ITest
{
List<string> TestList { get; }
}
public static class TestExtensions
{
private const string Old = "Old";
private const string New = "New";
public static void ManipulateTestList(this ITest test)
{
for (int i = 0; i < test.TestList.Count; i++)
{
test.TestList[i] = test.TestList[i].Replace(Old, New);
}
}
}
public class Tester : ITest
{
private List<string> testList = new List<string>();
public List<string> TestList
{
get { return testList; }
}
public Tester()
{
testList.Add("OldOne");
testList.Add("OldTwo");
// Doesn't work
// ManipulateTestList();
// Works
this.ManipulateTestList();
}
}
Invoking an extension method that works on a interface from an implementor seems to require the use of the this keyword. This seems odd.
Does anyone know why?
Is there an easier way to get shared implementation for an interface?
This irks me as I'm suffering multiple inheritance/mixin withdrawl.
Toy example:
public interface ITest
{
List<string> TestList { get; }
}
public static class TestExtensions
{
private const string Old = "Old";
private const string New = "New";
public static void ManipulateTestList(this ITest test)
{
for (int i = 0; i < test.TestList.Count; i++)
{
test.TestList[i] = test.TestList[i].Replace(Old, New);
}
}
}
public class Tester : ITest
{
private List<string> testList = new List<string>();
public List<string> TestList
{
get { return testList; }
}
public Tester()
{
testList.Add("OldOne");
testList.Add("OldTwo");
// Doesn't work
// ManipulateTestList();
// Works
this.ManipulateTestList();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我直接向语言团队询问了这个确切的问题。我手头没有电子邮件,但基本上答案(来自 Mads,IIRC)是:
我个人而言,我希望它能够一致地工作 - 第一个似乎不是一个大问题(但是,我不不编写编译器),并且都不接近这样一个事实:通常
this.*
是一个可选的东西(可能会产生诸如本地代码风格指南之类的影响,即“你应该使用这个。
”)。I asked this exact question to the language team directly. I don't have the e-mail to hand, but basically the answer (from Mads, IIRC) was that it is:
Personally, I'd have liked it to work consistently - the first doesn't seem a big problem (but then, I don't write compilers), and neither approaches the fact that normally
this.*
is an optional thing (that may have influences such as local code style guidelines, i.e. "thou shalt usethis.
").语言规范中的相关部分说:
这清楚地表明扩展方法只能在表达式 (expr) 上调用。当然,这个表达式可以是“
this
”,但它必须存在。The relevant section in the language specification says:
This clearly says that extension methods can only be invoked on an expression (expr). This expression can, of course, be “
this
”, but it must be present.扩展方法是一种编译器技巧,适用于将调用重定向到另一个静态类中的静态方法的对象。 '这。是编译器传递静态方法的对象。非工作示例只是编译器告诉您该方法不是并且实例方法的范围仅限于该类。
Extension methods are a compiler trick that work on an object that redirect the call to a static method in another static class. 'this. is the object, that the compiler passes the static method. The non working example is simply the compiler telling you that the method is not and instance method scoped to the class.