Can I use Linfu dynamic proxy to proxy an interface with generic parameters?

248 views Asked by At

I am currently using Linfu to create dynamic proxys, and it works really well for normal interfaces. The problem is I now need to create a dynamic proxy for an interface with generic parameters. I do not know the types of the generic parameters (or even load the assemblies containing them) until runtime. Does anyone know if this is even possible?

1

There are 1 answers

0
Franchesca On BEST ANSWER

Ok, I can do it by calling MyProxyFactory.CreateProxy< T >() with reflection something like this:

        Type myGenericParam1 = myParam1.GetType();
        Type myGenericParam2 = myParam2.GetType();
        Type myGenericInterfaceType = typeof(IMyInterface<,>);
        Type myActualInterfaceType = myGenericInterfaceType.MakeGenericType(myGenericParam1, myGenericParam2);
        var proxyObjectContainer = typeof(MyProxyFactory).GetMethod("CreateProxy", new Type[] { }).MakeGenericMethod(new[] { myActualInterfaceType }).Invoke(null, new object[] { });

        var proxyObject = proxyObjectContainer.GetType().GetProperty("Proxy").GetValue(proxyObjectContainer, null);

Obviously if you need to pass parameters to the proxy factory constructor for setting up your interceptor then this also needs to be added into the line that creates the proxyObjectContainer.