簡體   English   中英

我可以使用Linfu動態代理使用通用參數代理接口嗎?

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

我目前正在使用Linfu創建動態代理,它對於普通接口確實非常有效。 問題是我現在需要為具有通用參數的接口創建動態代理。 在運行時之前,我不知道泛型參數的類型(甚至不加載包含它們的程序集)。 有誰知道這是否有可能?

好的,我可以通過如下所示調用MyProxyFactory.CreateProxy <T>()來做到這一點:

        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);

顯然,如果您需要將參數傳遞給代理工廠構造函數以設置攔截器,則還需要將其添加到創建proxyObjectContainer的行中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM