コード例 #1
0
ファイル: ArraysHelper.cs プロジェクト: xxdoc/SKS
        /// <summary>
        /// Initializes a multi-dimensional array.
        /// </summary>
        /// <typeparam name="A">The type of the array including the dimensions in the form 'string[,,]'.</typeparam>
        /// <param name="lengths">The length of each dimension.</param>
        /// <param name="lowerBounds">The lower bounds to use for each dimension.</param>
        /// <param name="constructorParams">The list of values to be sent to
        /// the constructor of the item type of the array.</param>
        /// <returns>A new multi-dimensional array with its values initialized to a default value.</returns>
        public static A InitializeArray <A>(int[] lengths, int[] lowerBounds, object[] constructorParams) where A : class
        {
            if ((lengths == null) || (lowerBounds == null) || (constructorParams == null))
            {
                throw new NullReferenceException("AIS-Exception. Either 'lengths', 'lowerBounds' or 'constructorParams' parameter is null");
            }

            Type arrayType = typeof(A);

            if (!arrayType.IsArray)
            {
                throw new Exception("AIS-Exception. Array type is expected as parameter");
            }

            Type itemType = arrayType.GetElementType();

            if (itemType == null)
            {
                throw new NullReferenceException("AIS-Exception. itemType for the array couldn't be resolved");
            }

            InitialValueProvider valueProvider = new InitialValueProvider(itemType, constructorParams);

            //Only Primitive types and strings can be initialized with the same value, for other types a new instance
            //will be used for each element
            if (itemType.IsPrimitive || itemType.Equals(typeof(string)))
            {
                return(InternalInitializeArray(lengths, lowerBounds, itemType, valueProvider.GetInitialValue()) as A);
            }
            return(InternalInitializeArray(lengths, lowerBounds, itemType, valueProvider) as A);
        }
コード例 #2
0
        /// <summary>
        /// Executes a RedimPreserve over an array.
        /// </summary>
        /// <typeparam name="TA">The type of the array including the dimensions, for instance 'string[,,,]'.</typeparam>
        /// <param name="arraySource">The source array.</param>
        /// <param name="lengths">The length of the new dimensions.</param>
        /// <param name="lowerBounds">The lower bound of the new dimensions.</param>
        /// <returns>The new array with the elements of the old one.</returns>
        public static TA RedimPreserve <TA>(TA arraySource, int[] lengths, int[] lowerBounds) where TA : class
        {
            Array res;

            Type arrayType;
            Type arrayElementType = null;

            if (typeof(TA).ToString() == "System.Array")
            {
                arrayType = null;
                Array array1 = arraySource as Array;
                if (array1 != null)
                {
                    arrayElementType = array1.GetValue(0).GetType();
                }
            }
            else
            {
                arrayType        = typeof(TA);
                arrayElementType = arrayType.GetElementType();
            }

            if (arraySource == null)
            {
                return(InitializeArray <TA>(lengths, lowerBounds));
            }

            if (typeof(TA).ToString() != "System.Array")
            {
                RunRedimPreserveVerifications(arraySource, arrayType, lengths, lowerBounds);
            }
            Array array = arraySource as Array;

            //There is something to copy
            if (array != null)
            {
#if PORTABLE
                res = Array.CreateInstance(arrayElementType, lengths);
#else
                res = Array.CreateInstance(arrayElementType, lengths, lowerBounds);
#endif
                InitialValueProvider valueProvider = new InitialValueProvider(arrayElementType, null);
                //Multiple dimensions
                if (array.Rank > 1)
                {
                    FillsMultiDimensionalArray(array, res, valueProvider);
                }
                else
                {
                    FillsOneDimensionArray(array, res, valueProvider);
                }
            }
            else
            {
                res = InitializeArray <TA>(lengths, lowerBounds) as Array;
            }
            return(res as TA);
        }
コード例 #3
0
ファイル: ArraysHelper.cs プロジェクト: xxdoc/SKS
        /// <summary>
        /// Fills the n-dimension targetArray with either matching cell values from
        /// sourceArray or with a initial value.
        /// </summary>
        /// <param name="sourceArray">The array object containing the values to copy.</param>
        /// <param name="targetArray">The new array where to copy the values.</param>
        /// <param name="valueProvider">a <c>InitialValueProvider</c> object used to get
        /// the default values for the new cells.</param>
        private static void FillsMultiDimensionalArray(Array sourceArray, Array targetArray, InitialValueProvider valueProvider)
        {
            int rowsToCopy = GetFirstDimensionsSize(sourceArray);
            int originalLastDimensionSize = GetLastDimensionSize(sourceArray);
            int newLastDimensionSize      = GetLastDimensionSize(targetArray);
            int newCells   = newLastDimensionSize - originalLastDimensionSize;
            int lowerBound = sourceArray.GetLowerBound(0);
            // creates a new array with same dimensions than the target array (but a smaller one version) to hold the
            // default values to copy
            Array arrayLen = Array.CreateInstance(typeof(Int32), targetArray.Rank);

            for (int i = 0; i < targetArray.Rank - 1; i++)
            {
                arrayLen.SetValue(1, i);
            }
            arrayLen.SetValue(targetArray.GetLength(targetArray.Rank - 1), targetArray.Rank - 1);
            Array defaultValues = Array.CreateInstance(targetArray.GetType().GetElementType(), (int[])arrayLen);

            for (int i = 0; i < rowsToCopy; i++)
            {
                //copies the values from source array to target aray
                Array.Copy(sourceArray, (i * originalLastDimensionSize) + lowerBound, targetArray, (i * newLastDimensionSize) + lowerBound,
                           Math.Min(originalLastDimensionSize, newLastDimensionSize));
                // fills the remaining cells with the default value
                if (newCells > 0)
                {
                    for (int k = 0; k < arrayLen.Length; k++)
                    {
                        arrayLen.SetValue(0, k); // initilizing the indixes to first array element (0 on all dimensions)
                    }
                    // sets up the default values array with new values (we delegate to the value provider the responsability to get either
                    // a new or an old instance.
                    for (int j = originalLastDimensionSize; j < newLastDimensionSize; j++)
                    {
                        arrayLen.SetValue(j - originalLastDimensionSize, arrayLen.Length - 1); // moves the index
                        defaultValues.SetValue(valueProvider.GetInitialValue(), (int[])arrayLen);
                    }
                    Array.Copy(defaultValues, 0, targetArray,
                               (i * newLastDimensionSize) + originalLastDimensionSize + lowerBound, newCells);
                }
            }
        }
コード例 #4
0
ファイル: ArraysHelper.cs プロジェクト: xxdoc/SKS
 /// <summary>
 /// Fills the one-dimension targetArray with either matching cell values from
 /// sourceArray or with a initial value.
 /// </summary>
 /// <param name="sourceArray">The array object containing the values to copy.</param>
 /// <param name="targetArray">The new array where to copy the values.</param>
 /// <param name="valueProvider">a <c>InitialValueProvider</c> object used to get
 /// the default values for the new cells.</param>
 private static void FillsOneDimensionArray(Array sourceArray, Array targetArray, InitialValueProvider valueProvider)
 {
     Array.Copy(sourceArray, sourceArray.GetLowerBound(0), targetArray, sourceArray.GetLowerBound(0),
                Math.Min(targetArray.GetLength(0), sourceArray.GetLength(0)));
     if (targetArray.Length > sourceArray.Length)
     {
         for (int i = sourceArray.Length; i < targetArray.Length; i++)
         {
             targetArray.SetValue(valueProvider.GetInitialValue(), i + targetArray.GetLowerBound(0));
         }
     }
 }
コード例 #5
0
 /// <summary>
 /// Fills the one-dimension targetArray with either matching cell values from
 /// sourceArray or with a initial value.
 /// </summary>
 /// <param name="sourceArray">The array object containing the values to copy.</param>
 /// <param name="targetArray">The new array where to copy the values.</param>
 /// <param name="valueProvider">a <c>InitialValueProvider</c> object used to get
 /// the default values for the new cells.</param>
 /// <param name="bFixedLengthString">Optional bolean flag that indicates if the current array type is fixed length string (original vb6 code) -
 /// its default value is false, because it will be generated in upgraded code when current array type used to be a fixed length string.</param>
 /// <param name="iFixedLengthStringSize">Optional integer value that indicates what is the fixed length string size, used in conjunction with previous (bFixedLengthString) parameter.</param>
 private static void FillsOneDimensionArray(Array sourceArray, Array targetArray, InitialValueProvider valueProvider, bool bFixedLengthString = false, int iFixedLengthStringSize = 0)
 {
     if (targetArray.Length > sourceArray.Length)
     {
         for (int i = sourceArray.Length; i < targetArray.Length; i++)
         {
             targetArray.SetValue(valueProvider.GetInitialValue(bFixedLengthString, iFixedLengthStringSize), i + targetArray.GetLowerBound(0));
         }
     }
     Array.Copy(sourceArray, sourceArray.GetLowerBound(0), targetArray, sourceArray.GetLowerBound(0),
                Math.Min(targetArray.GetLength(0), sourceArray.GetLength(0)));
 }