I have a class that is auto generated with almost 7000 methods (never mind the contents or rationale). I would like to create a dynamic proxy for it.
I know of two options:
RealProxy.GetTransparentProxy()
Castle.DynamicProxy.ProxyGenerator
I would like to explore the second option. And so here is my code:
public class DynamicProxyFactory
{
public static readonly DynamicProxyFactory Instance = new DynamicProxyFactory();
private readonly ProxyGenerator m_generator;
private DynamicProxyFactory()
{
m_generator = new ProxyGenerator();
}
public TInterface GetProxy<TInterface>(TInterface target, IInterceptor interceptor)
{
Assert.IsTrue(typeof(TInterface).IsInterface);
var sw = Stopwatch.StartNew();
try
{
return (TInterface) m_generator.CreateInterfaceProxyWithTargetInterface(typeof(TInterface), target, interceptor);
}
finally
{
Trace.WriteLine($"Dynamic proxy generation tool {sw.Elapsed}");
}
}
}
The code works, but the up-front cost of generating the proxy is quite high - it measures in minutes. I understand, 7000 methods is not a joke.
What are my options to improve the performance? After all, not all the 7000 methods are called at once. So if I could generate the proxy methods lazily on-demand that would be a huge win. Is it possible? Anything I am missing here (besides the fact of having 7,000 methods, which is currently a given)? Maybe I should use a different proxy implementation?
My first suggestion by far would be to stop and try to get out of using generated code (especially if it creates a 7000 method monstrosity). Seriously.
Failing that, my second suggestion would be to try to do whatever your actual goal is by extending the generated code, rather than wrapping it in a proxy. It will almost certainly yield better results (performance, maintainability etc)
Failing that, if you're really set on using DP you might want to look at generating the proxy at build time and persisting the proxy assembly. I wrote a tutorial about it years ago. The API may be slightly different now but the basic structure and idea is the same.