public class MyBase
{
private void FooInternal()
{
DoRequiredStuff();
Foo();
}
public virtual void Foo() {}
}
Nowadays I don't think that consumers of a class that override a method should ever need to call base.Method(). The code should be written in such way that it cannot be broken.
public class MyBase
{
private void FooInternal()
{
DoRequiredStuff();
Foo();
}
public virtual void Foo() {}
}
If you are requiring that consumers of your class MUST implement functionality of a particular method, that method should be marked abstract.
If consumers of your class should optionally provide functionality of a particular method, that method should be virtual.
There is really no way to require that a consumer of a class call a base.Method() on a virtual method. It really depends on context. If the base.Method() does some work that you'd otherwise have to do, it'd behoove you to call base.Method() if that would save you some development time/it makes sense in that context.
It depends on whether the underlying functionality needs to be used.
For example, if the base object has some generic database functionality that needs to be run, call the base method at the end. If your code overwrites some of the properties that the base method will set, rather call the base method first.
If there is no source code or documentation, Redgate's .NET Reflector can just unpack the assemblies you are trying to use and you can see how the code works.
发布评论
评论(3)
如今,我认为重写方法的类的使用者不需要调用 base.Method()。代码应该以无法被破坏的方式编写。
Nowadays I don't think that consumers of a class that override a method should ever need to call base.Method(). The code should be written in such way that it cannot be broken.
如果您要求类的使用者必须实现特定方法的功能,则该方法应标记为抽象。
如果您的类的使用者应选择提供特定方法的功能,则该方法应该是虚拟的。
实际上没有办法要求类的使用者在虚拟方法上调用 base.Method() 。这确实取决于上下文。如果 base.Method() 做了一些您本来必须做的工作,那么您应该调用 base.Method() 如果这可以节省您一些开发时间/在这种情况下有意义。
If you are requiring that consumers of your class MUST implement functionality of a particular method, that method should be marked abstract.
If consumers of your class should optionally provide functionality of a particular method, that method should be virtual.
There is really no way to require that a consumer of a class call a base.Method() on a virtual method. It really depends on context. If the base.Method() does some work that you'd otherwise have to do, it'd behoove you to call base.Method() if that would save you some development time/it makes sense in that context.
取决于是否需要使用底层功能。
例如,如果基础对象具有一些需要运行的通用数据库功能,请在最后调用基础方法。如果您的代码覆盖了基本方法将设置的某些属性,请先调用基本方法。
如果没有源代码或文档,Redgate 的 .NET Reflector只需解压您尝试使用的程序集,您就可以看到代码是如何工作的。
It depends on whether the underlying functionality needs to be used.
For example, if the base object has some generic database functionality that needs to be run, call the base method at the end. If your code overwrites some of the properties that the base method will set, rather call the base method first.
If there is no source code or documentation, Redgate's .NET Reflector can just unpack the assemblies you are trying to use and you can see how the code works.