There are several ways to achieve this:
Code Contracts:
If you use Microsoft Contract Contract Extensions , you can set the flag to method1, which may be required inmethod2
private bool hasRunMethod1 = false;
public void method1()
{
Contract.Ensures( this.hasRunMethod1 );
hasRunMethod1 = true;
}
public void method2()
{
Contract.Requires( this.hasRunMethod1 );
}
hasRunMethod1 , . method1, postcondition Ensures.
:
(, Visual Studio ), . , :
abstract class Base {
private void method1()
{
}
private void method2()
{
}
protected abstract void BetweenMethod1And2();
public void RunTemplateMethod() {
method1();
BetweenMethod1And2();
method2();
}
}
Base BetweenMethod1And2 .
:
method1 , method2, . , method1 , , :
abstract class Token {};
class EncapsulatingClass {
private class PrivateToken : Token {};
public Token method1()
{
return new PrivateToken();
}
public void method2( Token token )
{
if ( ( token as PrivateToken ) == null ) {
throw new ArgumentException();
}
}
}