您好我正在使用C#中的TCP / IP套接字练习并遇到一个我不确定如何处理的故障。以下示例是使用自定义调度程序的客户端。当我尝试加载包含实现的程序集时,下面的异常是throw。
namespace TcpEchoServerThreadFactory
{
class TcpEchoServerThreadFactory
{
static void Main(string[] args)
{
if (args.Length != 3)
throw new ArgumentException("Parameter(s): [optional properties]" +
"<port> <protocol> <dispatcher>");
int port = Int32.Parse(args[0]);
string protocolName = args[1];
string dispatcherName = args[2];
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
ILogger logger = new ConsoleLogger();
System.Runtime.Remoting.ObjectHandle objHandle = Activator.CreateInstance("TcpEchoThreadServerLib", protocolName + "ProtocolFactory");
IProtocolFactory protocolFactory = (IProtocolFactory)objHandle.Unwrap();
objHandle = Activator.CreateInstance("TcpEchoThreadServerLib", dispatcherName + "Dispatcher");
IDispatcher dispatcher = (IDispatcher)objHandle.Unwrap();
dispatcher.startDispatching(listener, logger, protocolFactory);
}
}
}
Unhandled Exception: System.TypeLoadException: Could not load type 'EchoProtocolFactory' from assembly 'TcpEchoThreadServerLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=
null'.
at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Ob
ject[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(String assemblyName, String typeName)
at TcpEchoServerThreadFactory.TcpEchoServerThreadFactory.Main(String[] args) in C:\microsoft_press\DemoProjects\NonBlocking\TcpEchoServerThreadFactory\TcpEchoServerThreadFact
ory.cs:line 25
答案 0 :(得分:0)
根据Hans Passan的建议,我使用命名空间限定了类型,现在代码按预期工作
string ns = "TcpEchoThreadServerLibrary.";
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
ILogger logger = new ConsoleLogger();
System.Runtime.Remoting.ObjectHandle objHandle =
Activator.CreateInstance("TcpEchoThreadServerLib", ns + protocolName + "ProtocolFactory");
IProtocolFactory protocolFactory = (IProtocolFactory)objHandle.Unwrap();
objHandle = Activator.CreateInstance("TcpEchoThreadServerLib", ns + dispatcherName + "Dispatcher");
IDispatcher dispatcher = (IDispatcher)objHandle.Unwrap();
dispatcher.startDispatching(listener, logger, protocolFactory);