.NET 远程处理与反射
我需要动态加载在客户端远程处理上使用的接口程序集。像这样的东西。
static void Main(string[] args)
{
TcpClientChannel clientChannel = new TcpClientChannel();
ChannelServices.RegisterChannel(clientChannel, false);
Assembly interfaceAssembly = Assembly.LoadFile("RemotingInterface.dll");
Type iTheInterface =
interfaceAssembly.GetType("RemotingInterface.ITheService");
RemotingConfiguration.RegisterWellKnownClientType(iTheInterface,
"tcp://localhost:9090/Remotable.rem");
object wellKnownObject = Activator.GetObject(iTheInterface,
"tcp://localhost:9090/Remotable.rem");
}
只是我似乎无法掌握如何调用任何方法,因为我无法强制转换 Activator.GetObject。在编译时不知道接口的情况下如何创建 ITheService 的代理?
I need to dynamically load an interface assembly that I use on client-side remoting. Something like this.
static void Main(string[] args)
{
TcpClientChannel clientChannel = new TcpClientChannel();
ChannelServices.RegisterChannel(clientChannel, false);
Assembly interfaceAssembly = Assembly.LoadFile("RemotingInterface.dll");
Type iTheInterface =
interfaceAssembly.GetType("RemotingInterface.ITheService");
RemotingConfiguration.RegisterWellKnownClientType(iTheInterface,
"tcp://localhost:9090/Remotable.rem");
object wellKnownObject = Activator.GetObject(iTheInterface,
"tcp://localhost:9090/Remotable.rem");
}
Only I can't seem to grasp how to call any methods as I can't cast the Activator.GetObject. How can I create a proxy of ITheService without knowing the interface at compile-time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 MSDN 论坛得到答案< /a>.
Got an answer from MSDN forums.
返回的对象实现了接口,因此您可以使用反射来获取其成员方法并调用它们。
或者,在 C#4 中,您可以使用
dynamic
:The returned object implements the interface, so you can use reflection to get its member methods and invoke them.
Or, in C#4, you can use
dynamic
:首先,检查对象的可用方法/接口:
确定要调用的方法后,
方法[i].在对象上调用
。以下是一些示例:
First, inspect the available methods/interfaces of your object:
After you're sure about the method you want to invoke,
methods[i].Invoke
on the object.Here are some examples:
我可以从远程 URL 获取接口信息,如 http://localhost:8080/xxx.rem?wsdl< /a>.
作为WebService,我可以从服务url中获取接口信息, http://xXX.xx .xxx.xx/url.svc?wsdl,自己代码编译程序集,通过反射调用方法。
Can I get the Interface information from the remoting URL like http://localhost:8080/xxx.rem?wsdl.
As WebService, I can get the interface information from the service url, http://xXX.xx.xxx.xx/url.svc?wsdl, and compile the assembly by myself code, and invoke methods via reflection.