Getting the base type of an Array RRS feed

  • Question

  • I am trying to define a function that will 'drop' all of the dimentions of an array that are one. So int[2,1] -> int[2] and int[2,1,2] -> int[2,2]. This should work for all types of the array which brings me to the question. If I start the function like

            public static Array drop(Array x)
    . . . .
                  Type t = x.GetType();
    The type that I get back from GetType() is something like System.Int32[]. I want to find out what the individual element type is (System.Int32). I suppose I could turn the type name into a string and try to parse it but I was hoping that there was an easier way.

    The next problem is constructing the new array. I can iterate through the passed Array and with the aide of methods like Rank and GetLength I can get a list of the dimensions that are not 1. The problem now becomes consturcting a new Array with this list of dimensions. So say I know that dimensions 2 and 4 are not of length 1. For an integer array this would correspond to something like int[1,1,5,1,7]. So I need to construct an Array of integers (hence the first question) like int[5,7].

    Thank you for your ideas.

    Thursday, January 21, 2010 6:42 PM