Search code examples
c#.netcastle-dynamicproxydynamic-proxy

Is it possible to create dynamic proxies without having base class or interface?


Is it possible to create a dynamic proxy using common libraries like Castle Dynamic Proxy without having any base class or interface? I'm indeed interested to have dynamic on-the-fly classes in run-time.


Solution

  • Sure, use Reflection.Emit to bake your interface, then pass the generated interface type to CreateInterfaceProxyWithoutTarget and provide interceptors.

    Just keep in mind that you cannot unload a type, only an appdomain, so if you are going to create thousands of types you might end-up in a world of hurt performance-wise.