I’m guessing that this post will be useful to a very small number of people if at all. However there might be a bigger lesson here for people writing their own collections and that is how not to code GetEnumerator method.
but first some background information.
when working with a visual studio add-in I needed find out weather a specific project is excluded from the build cycle.
the DTE provide interfaces and object to access this info and the code I ended up with looked like this:
However, this code resulted in an exception popping from time to time which took me a little while to understand.
the exception I got was :
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
where the line number pointed to the foreach statement.
After some digging I found that this happens if in the solution you have an unloaded project as part of the solution. and the fix was quite simple jut replace the foreach with a simple for resulting in something like:
I think this is a mistake, in general if a piece of the information is missing don’t fail the entire operation just exclude that piece from the result. that way you will enable the user to decide what to do with the info he does have.
what do you think?