Wednesday, March 18, 2009

System.TypeLoadException: Could not load type

Problem:
Got a wonderful little error yesterday that had me pulling out my hair of which I have less and less each day. It starts with "System.TypeLoadException: Could not load type" and ends only after a good 4 or 5 hours of trying to deduce the problem through my code.
Here's a little background:
  1. VS.net 2008 Desktop Application (2.0 framework, 9.0.21022.8 RTM)
  2. Class library for container objects sent
  3. Class library for data layer which pulls information, creates the container objects, then returns the container objects to the caller.
Reproduction:
Simple enough. Here is how I reproduce the error:
  1. Have a container class library project with one class in it.
  2. Have a desktop app project with a datasource associated to that container project's single class.
  3. Recompile
  4. Close solution
  5. Open solution
  6. Recompile
  7. Add a new container class in the container Class library called Test.
  8. Rebuild your desktop app which should already have a project reference to the container class library.
  9. Add a new datasource pointing to that new object called Test.
  10. Create a new User Control called ctlTestControl
  11. Drag the datasource item 'Test' to the ctlTestControl. You should now have a datagrid with the container class's public fields as columns in said grid.
  12. Recompile
  13. Now attempt to add the control from your toolbox in your desktop apps Components section of the Toolbox.
  14. This is where I get the fun error:
Failed to create component: 'ctlTestControl'. The error message follows:
'System.TypeLoadException: Could not load type '.......

Research:
Here is where I got the answer to my problem: http://support.microsoft.com/kb/873267
The title of this kb article is:

You may receive a System.TypeLoadException exception when you try to preview a report in Report Designer after you install SQL Server 2000 Reporting Services Service Pack 1 (SP1)

This directed me to C:\Document
s and Settings\username\Local Settings\Application Data\Microsoft\VisualStudio\9.0\ProjectAssemblies .
What I noticed from looking at the files in the directories under ProjectAssemblies is that every time I rebuild my app it adds more directories and leaves the older compiled dll directories. So every time you rebuild the app, a new directory will appear in ProjectAssemblies, at least when you are developing a desktop app(haven't checked web apps, etc).

Resolution:
To resolve this issue:
  1. Closed solution (which happenned to delete all the associated subdirectories of ProjectAssemblies as described above)
  2. Openned solution
  3. Recompiled
  4. Successfully added control to form
Why work now? The app was caching an older version dll in the ProjectAssemblies directory. Closing the solution and reopenning the solution resolved it for me. Lol? could have saved myself by just closing the solution and reopenning it. :)

Perhaps my install is out of date, perhaps I have a setting out of whack, don't really care. All I care is that my...

Fury is contained...