我目前正在使用Castle Windsor构建示例应用程序。座右铭是使用xml / app.config来打开/关闭方法拦截。我之前使用过Fluent API,它很吸引人。下一步,我尝试将fluent API替换为xml。
代码的主要内容如下:一个名为RandomOperations的类,带有两个虚拟方法。一个实现IInterceptor的LoggingAspect类。一个MyInterceptorsSelector类,该类实现IModelInterceptorsSelector A Program.cs,该Program.cs以前具有流畅的api语法,现在仅用于对RandomOperations类的方法进行调用。一个名为app.config的部分,其中包含注册组件的xml语法。
使用流畅的api时,我可以截获方法调用,但无法使用xml / app.config注册来完成。有人可以告诉我们错过了什么吗?
这些类如下:
RandomOperations.cs
public class RandomOperations
{
public virtual int MyRandomMethod(int x)
{
return x * x;
}
public virtual void Writer(string x)
{
Console.WriteLine(x);
}
}
Run Code Online (Sandbox Code Playgroud)
LoggingAspect.cs
public class LoggingAspect : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Intercepted the call to " + invocation.Method.Name);
invocation.Proceed();
Console.WriteLine("After the method call, the return value is " + invocation.ReturnValue);
}
}
Run Code Online (Sandbox Code Playgroud)
MyInterceptorsSelector.cs
public class MyInterceptorsSelector : IModelInterceptorsSelector
{
public bool …Run Code Online (Sandbox Code Playgroud)