gpt4 book ai didi

c# - 使用反射的 .NET 远程处理

转载 作者:行者123 更新时间:2023-11-30 16:29:59 25 4
gpt4 key购买 nike

我需要动态加载我在客户端远程处理中使用的接口(interface)程序集。像这样。

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。如何在编译时不知道接口(interface)的情况下创建 ITheService 的代理?

最佳答案

MSDN forums得到了答案.

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

MethodInfo m = iTheInterface.GetMethod("MethodName");
m.Invoke(wellKnownObject, new object[] { "Argument"});
}

关于c# - 使用反射的 .NET 远程处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6023361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com