C# 如何在WinForms中向方法添加方面?
我继承了一个用WinForms编写的大型项目,在这个项目中我必须进行很多小的更改。我想使用AOP(Castle.DynamicProxy和Autofac),到目前为止还没有给我带来问题。我写了一个简单的方面:C# 如何在WinForms中向方法添加方面?,c#,winforms,aop,autofac,castle-dynamicproxy,C#,Winforms,Aop,Autofac,Castle Dynamicproxy,我继承了一个用WinForms编写的大型项目,在这个项目中我必须进行很多小的更改。我想使用AOP(Castle.DynamicProxy和Autofac),到目前为止还没有给我带来问题。我写了一个简单的方面: class TestAspect : IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine("Satrt method" + invocation.M
class TestAspect : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Satrt method" + invocation.Method.Name);
invocation.Proceed();
Console.WriteLine("Stop funkcji " + invocation.Method.Name);
}
}
接下来,我在ContainerBuilder中注册类型。例如:
builder.RegisterType<SomeClass>().As<ISome>().EnableInterfaceInterceptors().InterceptedBy(typeof(TestAspect));
builder.RegisterType<TestAspect>();
builder.RegisterType().As().EnableInterfaceInterceptors().Intercepters(typeof(TestSpect));
RegisterType();
到目前为止,一切顺利。但我必须将此方面添加到遗留代码中(编写得不好)。我必须将方面代码(不仅仅是上面的)显式地添加到WinForms中。例如,我有一个启动方法CalculateMething的按钮(在当前窗口中,而不是在类中)。在ContainerBuilder中,我注册当前表格:
builder.RegisterType<Form1>().EnableClassInterceptors().InterceptedBy(typeof(TestAspect));
builder.RegisterType().EnableClassInterceptors().InterceptBy(typeof(TestSpect));
但EnableClassInterceptors()仅适用于虚拟方法。
我知道旧代码需要重构,但客户已经在使用这个程序了。
我们重写了这段代码,但我们需要快速做一些更改。
我有两个解决方案:
也许有人知道吗?Castle DynamicProxy受到.NET多态性特性的限制。这意味着要么是一个接口代理,它实际上是实现类周围的装饰器,要么是一个类代理,它将实现类划分为子类并覆盖虚拟方法 另一个选择是某种编译后IL编织,将行为添加到IL中。这最终看起来像是你的第二个建议,只是它是由程序而不是程序员来完成的。调查