I recently needed to build C# specific name (which must always include global:: specifier) for an arbitrary type and have come accross following issue:
// 1 - value: System.String[,,,][,,][,] string unexpectedFullName = typeof( string[,][,,][,,,] ).FullName; // 2 - value: System.String[,][,,][,,,] string expectedFullName = Type.GetType( "System.String[,][,,][,,,]" ).FullName;
I was expecting that returned value would be same in both cases. However, for some reason the array related part of the value seems to be reversed (case 1). Is this reversal expected behavior?
While the value returned by
Type.FullName and the C# type identifier sometimes happen to be the same, this is not guaranteed. Keep in mind that
Type.FullName returns the same value regardless of what CLI language it is called from, be it C#, VB.NET, Oxygene or anything else.
For multidimensional and jagged arrays, C# syntax lists the indices in the order they are written later on, while reflection syntax returns something that matches the logical structure of the array. And a (C#)
string[,][,,][,,,] is, after all, a value of type
string, thereof a 4-dimensional array (i.e.
string[,,,]), thereof a 3-dimensional array (i.e.
string[,,,][,,]) and thereof a 2-dimensional array (i.e.
Rather than relying on the reflection syntax name returned by
FullName, you might want to examine the properties of the
Type class when analyzing types. Information such as the number of dimensions or the generic arguments can be retrieved from there.
When constructing types, you can also use methods such as
MakeGenericType to create complex types at runtime without constructing a string that contains the ingredients for the new types.
Some of the contents of this answer was pointed out by Marc Gravell - thank you!