5

I'd like to create dynamic proxy for a type in the BCL that is an abstract class with an internal constructor. I've been castle's dynamic proxy and this fails with an exception stating there is no parameterless constructor (their is - it's internal).

Is there any way to achieve this with castle? If not are any of the other dynamic proxy frameworks able to do this? This is the beginning of a development, so it would be easy to change frameworks.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Robert
  • 6,407
  • 2
  • 34
  • 41
  • 1
    See [System.Runtime.Remoting.Proxies.RealProxy](http://msdn.microsoft.com/en-us/library/system.runtime.remoting.proxies.realproxy.aspx) – L.B Apr 07 '12 at 13:15

2 Answers2

4

DynamicProxy doesn't do anything you couldn't do by hand in C#. So it can't inherit from types that it can not construct, including types that have no constructors accessible to DynamicProxy.


If you own the assembly you can provide DynamicProxy access via your AssemblyInfo.cs by adding:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

The important thing to note is the literal value DynamicProxyGenAssembly2 this is an in memory assembly created by DynamicProxy and you need to provide it access.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
0

I just found a way to achieve that, without modifying the target assembly.

The key is that TypeBuilder.DefineConstructor allows you to create a constructor without calling any constructor of the base. Therefore it's possible to dynamicly create a FAKE class inheriting the original class, with a public constructor.

Then just create proxy from the fake class. You could record the real target object in your IInterceptor object, or in some field of the fake class.

By the way, I use this trick to log ILGenerator.Emit calls. Anyone knows simpler ways to disassemble ILGenerator.m_ILStream?

kevinjwz
  • 181
  • 6