嗨,我在C#中完成了来自TCP/IP套接字的练习,并经历了一个失败,我不知道该如何处理。下面的示例是使用自定义调度程序的客户端。当我试图加载包含实现的程序集时,下面的异常将被抛出。
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发布于 2016-06-26 06:50:37
按照Hans的建议,我用命名空间对类型进行了限定,现在代码按预期的方式工作。
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);https://stackoverflow.com/questions/37905506
复制相似问题