开发者

Create dynamic proxies on System.Type

开发者 https://www.devze.com 2023-04-06 07:50 出处:网络
I have List<Type>, here Type is interface which i got using reflection. So how to create the wcf proxy using channnel factory on these Type.

I have List<Type>, here Type is interface which i got using reflection. So how to create the wcf proxy using channnel factory on these Type.

like:

foreach(Type t in types)
{
t proxy = ChannelFactory<t>.CreateChannel(new NetTcpBinding(), 
             new EndpointAddress(serviceAddress));
}

Here t开发者_如何学Python is interface but the above code is giving compiler error.Can anybody tell me how to create wcf service proxy on Type.


You can use reflection and call the method Type.MakeGenericType:

foreach (Type t in types)
{
    Type genType = typeof(ChannelFactory<>).MakeGenericType(t);

    MethodInfo createChannelMethod = genType.GetMethod("CreateChannel", 
                                        new[] { typeof(Binding),
                                                typeof(EndpointAddress) });

    var proxy = createChannelMethod.Invoke(
                                null, 
                                BindingFlags.Static, 
                                null, 
                                new object[] { 
                                    new NetTcpBinding(), 
                                    new EndpointAddress(serviceAddress) 
                                }, 
                                null);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消