C# 在系统上创建动态代理。键入
我有C# 在系统上创建动态代理。键入,c#,wcf,reflection,C#,Wcf,Reflection,我有列表,这里的类型是我使用反射得到的接口。 因此,如何在这些类型上使用Channel factory创建wcf代理 比如: foreach(Type t in types) { t proxy = ChannelFactory<t>.CreateChannel(new NetTcpBinding(), new EndpointAddress(serviceAddress)); } foreach(类型中的类型t) { t proxy=ChannelFa
列表
,这里的类型是我使用反射得到的接口。
因此,如何在这些类型上使用Channel factory创建wcf代理
比如:
foreach(Type t in types)
{
t proxy = ChannelFactory<t>.CreateChannel(new NetTcpBinding(),
new EndpointAddress(serviceAddress));
}
foreach(类型中的类型t)
{
t proxy=ChannelFactory.CreateChannel(新的NetTcpBinding(),
新端点地址(serviceAddress));
}
这里是接口,但上面的代码给出了编译器错误。有人能告诉我如何在类型上创建wcf服务代理吗。您可以使用反射并调用方法
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);
}
foreach(类型中的类型t)
{
Type genType=typeof(ChannelFactory);
MethodInfo createChannelMethod=genType.GetMethod(“CreateChannel”,
新[]{typeof(Binding),
类型(端点地址)});
var proxy=createChannelMethod.Invoke(
无效的
BindingFlags.Static,
无效的
新对象[]{
新的NetTcpBinding(),
新端点地址(serviceAddress)
},
无效);
}
您可以使用反射并调用方法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);
}
foreach(类型中的类型t)
{
Type genType=typeof(ChannelFactory);
MethodInfo createChannelMethod=genType.GetMethod(“CreateChannel”,
新[]{typeof(Binding),
类型(端点地址)});
var proxy=createChannelMethod.Invoke(
无效的
BindingFlags.Static,
无效的
新对象[]{
新的NetTcpBinding(),
新端点地址(serviceAddress)
},
无效);
}
Thx用于快速回复。使用上述代码时,我得到编译器错误为“未找到隐式类型数组的最佳类型”。我已更新了答案。只需编写newobject[]
而不是new[]
就可以通过将其设置为new object[]{new NetTcpBinding(),new EndpointAddress(serviceAddress)}Thx来快速回复。使用上述代码时,我得到的编译器错误是“没有找到隐式类型数组的最佳类型”。我已更新了答案。只需编写newobject[]
而不是new[]
通过将其设置为新对象[]{new NetTcpBinding(),new EndpointAddress(serviceAddress)}来解决问题