dcrearer
dcrearer

Reputation: 2072

System.TypeLoadException

Hi I working through exercises from TCP/IP Sockets in C# and experiencing a failure which I'm not sure how to approach. The example below is the client that uses a custom dispatcher. When I try to load the assembly that contains the implementation the exception below is 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

Upvotes: 0

Views: 1953

Answers (1)

dcrearer
dcrearer

Reputation: 2072

As recommended by Hans Passan I qualified the type with the namespace, now the code works as expected

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

Upvotes: 1

Related Questions