Geeta
Geeta

Reputation: 79

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.

Upvotes: 0

Views: 208

Answers (1)

Jan
Jan

Reputation: 16038

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

Upvotes: 3

Related Questions