Anybody seen this exception before, Google doesn’t have a single post regarding the exception.
The code that raises the error is a simple add.
Items.Add(item);
System.TypeLoadException: Bad flags on delegate constructor.
at System.Windows.Forms.ListView.Sort()
at System.Windows.Forms.ListView.InsertItems(Int32 displayIndex, ListViewItem[] items, Boolean checkHosting)
at System.Windows.Forms.ListView.ListViewNativeItemCollection.Add(ListViewItem value)
at System.Windows.Forms.ListView.ListViewItemCollection.Add(ListViewItem value)
The cause is a System.Windows.Form v2.0 bug I identified this morning (and that is fixed in System.Windows.Form v4.0).
For me it happened while my code adds an
Itemto theListBoxwhile the hosting process is shutting down. The private instance fieldListBox.listItemsArrayis null and this provokes theNullReferenceException. I don’t know exactly whyListBox.listItemsArrayis null, but I’d guess it is related to a handle creation problem.For me the workaround was easy since a
try/catchwas enough because the process is shutting down anyway. You can certainly dig ino the problem with the decompiling Reflector feature as I did: