我想声明一个接口,从它继承的人将自动执行2个操作1-将在函数开始时写入日志
2-将在函数结束时写入日志,这些操作将自动完成,程序员应该做的唯一一件事就是在接口上实现函数,有些人知道我应该如何实现它?
发布于 2015-05-29 02:10:46
您不能使用接口做到这一点,但是您可以提供一个接口的实现,该接口包装另一个实现并记录自己的函数调用。例如:
public interface IExample
{
void DoSomething(string parameter1);
}
public class ExampleImpl : IExample
{
private IExample actualImplementation;
public ExampleImpl(IExample actualImplementation)
{
this.actualImplementation = actualImplementation;
}
public void DoSomething(string parameter1)
{
//Code to log function begin here
this.actualImplementation.DoSomething(parameter1);
//Code to log function end here
}
}现在,假设另一个程序员也实现了该接口,例如,假设他们的实现称为AnotherProgrammersImplementation
IExample thisObjectLogsFunctionCalls = new ExampleImpl(new AnotherProgrammersImplementation());
thisObjectLogsFunctionCalls.DoSomething("test string");https://stackoverflow.com/questions/30513930
复制相似问题