/// <summary>
        /// Changes the array size.
        /// </summary>
        public void SetArraySize()
        {
            if (!CanSetArraySize)
            {
                return;
            }

            EndEdit();

            AccessInfo info = NavigationMENU.Items[NavigationMENU.Items.Count - 1].Tag as AccessInfo;

            TypeInfo currentType = info.TypeInfo;
            object currentValue = info.Value;

            if (info.Value is Variant)
            {
                Variant variant = (Variant)info.Value;
                currentValue = variant.Value;

                if (currentValue != null)
                {
                    currentType = variant.TypeInfo;

                    if (currentType == null)
                    {
                        currentType = TypeInfo.Construct(currentValue);
                    }
                }
            }

            int[] dimensions = null;

            Array array = currentValue as Array;

            if (array != null)
            {
                dimensions = new int[array.Rank];

                for (int ii = 0; ii < array.Rank; ii++)
                {
                    dimensions[ii] = array.GetLength(ii);
                }
            }

            IList list = currentValue as IList;

            if (array == null && list != null)
            {
                dimensions = new int[1];
                dimensions[0] = list.Count;
            }

            Matrix matrix = currentValue as Matrix;

            if (matrix != null)
            {
                dimensions = matrix.Dimensions;
                array = matrix.ToArray();
            }

            SetTypeDlg.SetTypeResult result = new SetTypeDlg().ShowDialog(currentType, dimensions);

            if (result == null)
            {
                return;
            }

            // convert to new type.
            object newValue = currentValue;

            if (result.ArrayDimensions == null || result.ArrayDimensions.Length < 1)
            {
                newValue = Convert(currentValue, currentType, result.TypeInfo, result.UseDefaultOnError);
            }
            else
            {
                if (array == null && list != null)
                {
                    Type elementType = GetListElementType(list);

                    for (int ii = result.ArrayDimensions[0]; ii < list.Count; ii++)
                    {
                        list.RemoveAt(ii);
                    }

                    for (int ii = list.Count; ii < result.ArrayDimensions[0]; ii++)
                    {
                        list.Add(Activator.CreateInstance(elementType));
                    }

                    newValue = list;
                }

                if (array != null)
                {
                    Array newArray = null;

                    if (currentValue is Array)
                    {
                        newArray = Array.CreateInstance(currentValue.GetType().GetElementType(), result.ArrayDimensions);
                    }
                    else
                    {
                        newArray = TypeInfo.CreateArray(result.TypeInfo.BuiltInType, result.ArrayDimensions);
                    }

                    int maxCount = result.ArrayDimensions[0];

                    for (int ii = 1; ii < result.ArrayDimensions.Length; ii++)
                    {
                        maxCount *= result.ArrayDimensions[ii];
                    }

                    int count = 0;

                    foreach (object element in array)
                    {
                        if (maxCount <= count)
                        {
                            break;
                        }

                        object newElement = Convert(element, currentType, result.TypeInfo, result.UseDefaultOnError);
                        int[] indexes = GetIndexFromCount(count++, result.ArrayDimensions);
                        newArray.SetValue(newElement, indexes);
                    }

                    newValue = newArray;
                }
            }

            NavigationMENU.Items.RemoveAt(NavigationMENU.Items.Count - 1);

            info.TypeInfo = result.TypeInfo;
            info.Value = newValue;
            ShowValue(info);
        }
示例#2
0
        /// <summary>
        /// Changes the array size.
        /// </summary>
        public void SetArraySize()
        {
            if (!CanSetArraySize)
            {
                return;
            }

            EndEdit();

            AccessInfo info = NavigationMENU.Items[NavigationMENU.Items.Count - 1].Tag as AccessInfo;

            TypeInfo currentType  = info.TypeInfo;
            object   currentValue = info.Value;

            if (info.Value is Variant)
            {
                Variant variant = (Variant)info.Value;
                currentValue = variant.Value;

                if (currentValue != null)
                {
                    currentType = variant.TypeInfo;

                    if (currentType == null)
                    {
                        currentType = TypeInfo.Construct(currentValue);
                    }
                }
            }

            int[] dimensions = null;

            Array array = currentValue as Array;

            if (array != null)
            {
                dimensions = new int[array.Rank];

                for (int ii = 0; ii < array.Rank; ii++)
                {
                    dimensions[ii] = array.GetLength(ii);
                }
            }

            IList list = currentValue as IList;

            if (array == null && list != null)
            {
                dimensions    = new int[1];
                dimensions[0] = list.Count;
            }

            Matrix matrix = currentValue as Matrix;

            if (matrix != null)
            {
                dimensions = matrix.Dimensions;
                array      = matrix.ToArray();
            }

            SetTypeDlg.SetTypeResult result = new SetTypeDlg().ShowDialog(currentType, dimensions);

            if (result == null)
            {
                return;
            }

            // convert to new type.
            object newValue = currentValue;

            if (result.ArrayDimensions == null || result.ArrayDimensions.Length < 1)
            {
                newValue = Convert(currentValue, currentType, result.TypeInfo, result.UseDefaultOnError);
            }
            else
            {
                if (array == null && list != null)
                {
                    Type elementType = GetListElementType(list);

                    for (int ii = result.ArrayDimensions[0]; ii < list.Count; ii++)
                    {
                        list.RemoveAt(ii);
                    }

                    for (int ii = list.Count; ii < result.ArrayDimensions[0]; ii++)
                    {
                        list.Add(Activator.CreateInstance(elementType));
                    }

                    newValue = list;
                }

                if (array != null)
                {
                    Array newArray = null;

                    if (currentValue is Array)
                    {
                        newArray = Array.CreateInstance(currentValue.GetType().GetElementType(), result.ArrayDimensions);
                    }
                    else
                    {
                        newArray = TypeInfo.CreateArray(result.TypeInfo.BuiltInType, result.ArrayDimensions);
                    }

                    int maxCount = result.ArrayDimensions[0];

                    for (int ii = 1; ii < result.ArrayDimensions.Length; ii++)
                    {
                        maxCount *= result.ArrayDimensions[ii];
                    }

                    int count = 0;

                    foreach (object element in array)
                    {
                        if (maxCount <= count)
                        {
                            break;
                        }

                        object newElement = Convert(element, currentType, result.TypeInfo, result.UseDefaultOnError);
                        int[]  indexes    = GetIndexFromCount(count++, result.ArrayDimensions);
                        newArray.SetValue(newElement, indexes);
                    }

                    newValue = newArray;
                }
            }

            NavigationMENU.Items.RemoveAt(NavigationMENU.Items.Count - 1);

            info.TypeInfo = result.TypeInfo;
            info.Value    = newValue;
            ShowValue(info);
        }