Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top