In any class, how do I explicitly refer to a certain method of my class?
在任何课程中,我如何明确地引用我班级的某种方法?
For example, this code works:
例如,此代码有效:
class Test : IEnumerable<T> {
public IEnumerator<T> GetEnumerator() { return null; }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
But this one doesn't!
但是这个没有!
class Test : IEnumerable<T> {
IEnumerator<T> IEnumerable<T>.GetEnumerator() { return null; }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } // error
}
How do I refer to the generic version of my method? In Java I would simply prefix it with the interface name, but this does't seem to work in C#. Is there a way to do this, other than ((IEnumerable<T>)this).GetEnumerator()
?
我如何参考我方法的通用版本?在Java中,我只是在其前面添加接口名称,但这似乎不适用于C#。有没有办法做到这一点,除了((IEnumerable
EDIT: I'm not interested in "why" it does't work this way. I'm just looking for a way to do it. Thanks.
编辑:我对“为什么”这种方式不起作用不感兴趣。我只是在寻找一种方法。谢谢。
2 个解决方案
#1
If you use explicit interface implementation, you need to access the method through that interface.
如果使用显式接口实现,则需要通过该接口访问该方法。
So no, there is no way other than what you propose in the text.
所以不,除了你在文中提出的建议外,别无他法。
In this case though you will typically not use explicit implementation for the generic interface.
在这种情况下,您通常不会对通用接口使用显式实现。
Edit: If the documentation warning is the source of your concerns, let me give you a much better advice: GhostDoc.
编辑:如果文档警告是您关注的来源,那么让我给您一个更好的建议:GhostDoc。
If you install this, and bind it to, say, Shift+Ctrl+D (default), then it will try to understand the name of your methods and add the necessary documentation. This isn't the good part, since you should always edit this part yourself, but if you inherit methods and properties from either a base class, or as in your case, an interface, it will copy the documentation automatically. Sometimes you need to edit it to make it more specific to your type, but usually you can just leave it.
如果您安装它,并将其绑定到Shift + Ctrl + D(默认),那么它将尝试了解您的方法的名称并添加必要的文档。这不是很好的部分,因为您应该自己编辑此部件,但如果您从基类继承方法和属性,或者在您的情况下继承,那么它将自动复制文档。有时您需要对其进行编辑以使其更适合您的类型,但通常您可以将其保留。
It will also have special detection of typical patterns.
它还将对特殊模式进行特殊检测。
Try it, I'm sure you'll love it.
尝试一下,我相信你一定会喜欢它。
#2
Well, you can cast...
好吧,你可以投...
IEnumerator<T> IEnumerable<T>.GetEnumerator() { return null; } // TODO
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable<T>)this).GetEnumerator();
}
or you can use a non-public instance method for both:
或者你可以使用非公共实例方法:
IEnumerator<T> IEnumerable<T>.GetEnumerator() { return GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
private IEnumerator<T> GetEnumerator() { return null; } // TODO
Personally, though, I prefer to see a public GetEnumerator() - same as I like a public Dispose() - it makes it easier for the consumer to know it is there. I don't think all interface implementations should be public (far from it), but foreach
and using
are so fundamental that a little advertising doesn't hurt.
但就个人而言,我更喜欢看到一个公共的GetEnumerator() - 就像我喜欢公开的Dispose()一样 - 它让消费者更容易知道它在那里。我不认为所有接口实现都应该是公开的(远非它),但foreach和using是如此基本,以至于一点广告不会受到伤害。
#1
If you use explicit interface implementation, you need to access the method through that interface.
如果使用显式接口实现,则需要通过该接口访问该方法。
So no, there is no way other than what you propose in the text.
所以不,除了你在文中提出的建议外,别无他法。
In this case though you will typically not use explicit implementation for the generic interface.
在这种情况下,您通常不会对通用接口使用显式实现。
Edit: If the documentation warning is the source of your concerns, let me give you a much better advice: GhostDoc.
编辑:如果文档警告是您关注的来源,那么让我给您一个更好的建议:GhostDoc。
If you install this, and bind it to, say, Shift+Ctrl+D (default), then it will try to understand the name of your methods and add the necessary documentation. This isn't the good part, since you should always edit this part yourself, but if you inherit methods and properties from either a base class, or as in your case, an interface, it will copy the documentation automatically. Sometimes you need to edit it to make it more specific to your type, but usually you can just leave it.
如果您安装它,并将其绑定到Shift + Ctrl + D(默认),那么它将尝试了解您的方法的名称并添加必要的文档。这不是很好的部分,因为您应该自己编辑此部件,但如果您从基类继承方法和属性,或者在您的情况下继承,那么它将自动复制文档。有时您需要对其进行编辑以使其更适合您的类型,但通常您可以将其保留。
It will also have special detection of typical patterns.
它还将对特殊模式进行特殊检测。
Try it, I'm sure you'll love it.
尝试一下,我相信你一定会喜欢它。
#2
Well, you can cast...
好吧,你可以投...
IEnumerator<T> IEnumerable<T>.GetEnumerator() { return null; } // TODO
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable<T>)this).GetEnumerator();
}
or you can use a non-public instance method for both:
或者你可以使用非公共实例方法:
IEnumerator<T> IEnumerable<T>.GetEnumerator() { return GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
private IEnumerator<T> GetEnumerator() { return null; } // TODO
Personally, though, I prefer to see a public GetEnumerator() - same as I like a public Dispose() - it makes it easier for the consumer to know it is there. I don't think all interface implementations should be public (far from it), but foreach
and using
are so fundamental that a little advertising doesn't hurt.
但就个人而言,我更喜欢看到一个公共的GetEnumerator() - 就像我喜欢公开的Dispose()一样 - 它让消费者更容易知道它在那里。我不认为所有接口实现都应该是公开的(远非它),但foreach和using是如此基本,以至于一点广告不会受到伤害。