嗨,我正在尝试使用统一拦截(我不想使用统一容器)。我能够配置运行时,但是不知道如何从config配置它。
我的代码:
public interface ICalculator
{
int Add(int first, int second);
int Multiply(int first, int second);
}
行为:
internal class LogBehavior : IInterceptionBehavior
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
// My Code
IMethodReturn result = getNext()(input, getNext);
return result;
}
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public bool WillExecute {
get { return true; }
}
}
这就是我所说的
public static void Main(string[] args)
{
var calculator = new Calculator();
var calculatorProxy = Intercept.ThroughProxy<ICalculator>(calculator,
new InterfaceInterceptor(), new[] { new LogBehavior() });
Console.WriteLine(calculatorProxy.Add(2, 2));
Console.ReadKey();
}
这正在工作。我需要从配置文件中进行配置。请帮忙
如果没有容器,则必须自己进行代理实例化。那是Intercept.ThroughProxy<>
电话。如果您正在使用另一个容器,则可以扩展它以读取配置并相应地处理它。
但是,没有什么魔术可以让.net / unity读取配置文件并执行new Calculator()
,Calculator
它将被配置文件中定义的拦截器代理和拦截。new
不能扩展以处理该问题,因此总会有一些组件需要您提供的实例ICalculator
,然后该实例将检查是否需要代理以及需要使用哪些拦截器...
当然,这是一个例外。如果使用编织(Fody,PostSharp),则可以在不使用动态代理的情况下进行AOP。然后,您实际上可以做到new Calculator()
,它将具有它的各个方面,而不仅仅是它的核心实现...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句