I would like to log the calling of user interface methods.
My idea is to intercept the MyUserInterface class which inherits from System.Windows.Forms.Form.
However, I got TypeLoadException: Type 'Castle.Proxies.IComponentProxy' from assembly 'DynamicProxyGenAssembly2' is attempting to implement an inaccessible interface
How to solve this issue?
Am I missing something on registration?
Thank you!
I also save the code on fiddle. You can try to run.
using System;
using Autofac;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
public class Program
{
public static void Main()
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<MyInterceptor>();
builder.RegisterType<MyUserInterface>().As<IMyUserInteraface>()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof(MyInterceptor));
var container = builder.Build();
var ui = container.Resolve<IMyUserInteraface>();
ui.Show();
}
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("UI intercepted and logged");
invocation.Proceed();
}
}
//If MyUserInterface inherit Form, will get TypeLoadException.
//TypeLoadException: Type 'Castle.Proxies.IComponentProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface.
//public class MyUserInterface : Form, IMyUserInteraface
public class MyUserInterface : IMyUserInteraface
{
public void Show()
{
Console.WriteLine("Show something");
}
}
public interface IMyUserInteraface
{
void Show();
}
I had tried to remove inheritance Form. Then it works. But MyUserInterface have to inherit from Form.
Code here
For now, solved by using CreateInterfaceProxyWithTarget in ProxyGenerator.