Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dynamic proxy for an abstract class with an internal constructor

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.

like image 325
Robert Avatar asked Oct 09 '22 04:10

Robert


1 Answers

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.

like image 108
Krzysztof Kozmic Avatar answered Oct 12 '22 01:10

Krzysztof Kozmic