Search code examples
.netproxycastle-dynamicproxy

DynamicProxy2 and Proxy Chaining


I have the need to proxy the property types of a proxy. So the case would be:

I have interface IMyInterface:

public interface IMyInterface
{
    public String Name {get; set;}
    public Int Id {get;set;}
}

I can mock the interface just fine but I want to be able to mock, for instance, the Name property. I realize that String cannot be mocked because it is sealed. The functionality that I would like to see would be:

IMyInterfaceMock.Name.Equals() 

should be handled by an Interceptor. I can't envision that this is even possible with the existing framework because I would be changing the type of the property but I was wondering if there was a clever way to achieve this. Is there any way I could interject into the proxy generation and modify the proxy's property's return type?

I don't think it's possible with DynamicProxy2 as it stands but I was wondering if anyone knew some magic.


Solution

  • I realize the type is going to be an invalid override. What I was really looking for was a way to generate a dynamic type. I accomplished this using the System.Reflection.Emit classes.

    I created a dynamic type where the property Types were that of a well-known type that I could then Intercept.

    I should have explained that I was writing the proxy object to a PowerShell pipeline and thus didn't really care about the type that was emitted. I just need a way to evaluate on the comparison operators.