在 System.Type 上创建动态代理

发布于 2024-12-05 02:04:22 字数 336 浏览 2 评论 0原文

我有 List,这里 Type 是我使用反射获得的接口。 那么如何在这些类型上使用通道工厂创建 wcf 代理。

就像:

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

这里 t 是接口,但上面的代码给出了编译器错误。任何人都可以告诉我如何在类型上创建 wcf 服务代理。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-12-12 02:04:22

您可以使用反射并调用方法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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文