简体   繁体   中英

Create dynamic proxies on System.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 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM