Rant about EnvDTE.CodeTypeRef.Rank

When you have a variable (EnvDTE.CodeVariable) or function (EnvDTE.CodeFunction), their Type property returns an EnvDTE.CodeTypeRef with information about the type. If the type is an array then CodeTypeRef.Rank returns the dimensions (1, 2…). The problem is that if it is not an array, it throwns an exception, which can hurt the performance if you do that with lots of functions or variables (think an operation that processes the whole code of a solution). It would be much more better if the automation model returns 0 for the Rank if the type is not an array, but chances of MS changing that at this point are minimal, so as a workaround, you should try to call the Rank property only if you know in advance that it is an array (and therefore it is not going to throw an exception). You can know using the CodeTypeRef.AsString property: if the result contains “(” for VB.NET or “[” for C#, you know it is an array.