發現我只能用反射來做到這一點。當然你也必須使用反射來調用這些方法。
創造「的ChannelFactory」,並稱之爲「CreateChannel」的方法:
private ChannelFactory CreateChannelFactory()
{
var channelFactoryType = typeof (ChannelFactory);
channelFactoryType = channelFactoryType.MakeGenericType(serviceType);
return (ChannelFactory)Activator.CreateInstance(channelFactoryType, binding, address);
}
private object CreateChannel()
{
var createchannel = channelFactory.GetType().GetMethod("CreateChannel", new Type[0]);
return createchannel.Invoke(channelFactory, null);
}
現在通道創建,但因爲只是接口類型是可用的,我只能得到方法調用:
var serviceType = service.GetType();
var remoteMethod = service.GetMethod(invocation.Method.Name);
remoteMethod.Invoke(service, invocation.Arguments);
我不得不做「typeof(ChannelFactory <>)」來使它工作 – Robin 2012-12-21 17:30:55