예제 #1
0
        private static void checkArrayDimensions(Array array, AdsDatatypeArrayInfo[] arrayInfo, bool exact)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            if (arrayInfo == null)
            {
                throw new ArgumentNullException("arrayInfo");
            }
            int  jagLevel           = 0;
            Type baseElementType    = null;
            int  jaggedElementCount = 0;

            if (PrimitiveTypeConverter.TryJaggedArray(array, out jagLevel, out baseElementType, out jaggedElementCount))
            {
                if (new DimensionCollection(arrayInfo).ElementCount != jaggedElementCount)
                {
                    throw new ArgumentException("Cannot convert dataType of symbol to this type. Jagged array mismatching!");
                }
            }
            else
            {
                if (arrayInfo.Length != array.Rank)
                {
                    throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array rank of {arrayInfo.Length}, actual rank is {array.Rank}", "type");
                }
                if (arrayInfo.Length == 1)
                {
                    if (exact && (arrayInfo[0].Elements != array.Length))
                    {
                        throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array of length {arrayInfo[0].Elements}, actual length is {array.Length}", "type");
                    }
                    if (!exact && (arrayInfo[0].Elements < array.Length))
                    {
                        throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array of length {arrayInfo[0].Elements}, actual length is {array.Length}", "type");
                    }
                }
                else if (arrayInfo.Length == 2)
                {
                    if (arrayInfo[0].Elements != array.GetLength(0))
                    {
                        throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array of length {arrayInfo[0].Elements}, actual length is {array.GetLength(0)}", "type");
                    }
                    if (arrayInfo[1].Elements != array.GetLength(1))
                    {
                        throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array of length {arrayInfo[1].Elements}, actual length is {array.GetLength(1)}", "type");
                    }
                }
                else
                {
                    if (arrayInfo.Length != 3)
                    {
                        throw new ArgumentException("Cannot convert ads array type of symbol to this type.", "type");
                    }
                    if (arrayInfo[0].Elements != array.GetLength(0))
                    {
                        throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array of length {arrayInfo[0].Elements}, actual length is {array.GetLength(0)}", "type");
                    }
                    if (arrayInfo[1].Elements != array.GetLength(1))
                    {
                        throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array of length {arrayInfo[1].Elements}, actual length is {array.GetLength(1)}", "type");
                    }
                    if (arrayInfo[2].Elements != array.GetLength(2))
                    {
                        throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array of length {arrayInfo[2].Elements}, actual length is {array.GetLength(2)}", "type");
                    }
                }
            }
        }
예제 #2
0
        internal int InitializeArray(Type managedType, TcAdsDataType adsType, AdsBinaryReader reader, int readerOffset, int jagLevel, out object value)
        {
            int num2;

            if (managedType == null)
            {
                throw new ArgumentNullException("managedType");
            }
            if (adsType == null)
            {
                throw new ArgumentNullException("adsType");
            }
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            int num = readerOffset;

            if (!managedType.IsArray)
            {
                throw new ArgumentException($"Cannot convert datatype of symbol to this type. Expected an array, actual type is {managedType.ToString()}", "type");
            }
            Type elementType = managedType.GetElementType();

            AdsDatatypeArrayInfo[] arrayInfo = adsType.ArrayInfo;
            Type baseElementType             = null;
            bool flag = PrimitiveTypeConverter.TryJaggedArray(managedType, out num2, out baseElementType);
            int  arrayElementCount = AdsArrayDimensionsInfo.GetArrayElementCount(arrayInfo);
            int  byteLength        = adsType.ByteSize / arrayElementCount;

            if (flag)
            {
                if (arrayInfo.Length != 1)
                {
                    throw new ArgumentException($"Cannot convert datatype of symbol to this type. Expected an array rank of {arrayInfo.Length}, actual rank is {num2}", "type");
                }
            }
            else
            {
                int length = arrayInfo.Length;
                if (length != managedType.GetArrayRank())
                {
                    throw new ArgumentException($"Cannot convert datatype of symbol to this type. Expected an array rank of {length}, actual rank is {managedType.GetArrayRank()}", "type");
                }
            }
            AdsArrayDimensionsInfo info = new AdsArrayDimensionsInfo(arrayInfo);
            Array array = null;

            if (flag)
            {
                jagLevel++;
                array = Array.CreateInstance(elementType, info.DimensionElements[jagLevel]);
                int[] lowerBounds = new int[] { info.LowerBounds[jagLevel] };
                int[] upperBounds = new int[] { info.UpperBounds[jagLevel] };
                foreach (int[] numArray in new ArrayIndexIterator(lowerBounds, upperBounds, true))
                {
                    object obj2 = null;
                    readerOffset += this.InitializeArray(elementType, (TcAdsDataType)adsType.BaseType, reader, readerOffset, jagLevel, out obj2);
                    array.SetValue(obj2, numArray);
                }
            }
            else
            {
                DataTypeCategory category = adsType.BaseType.Category;
                array = Array.CreateInstance(elementType, info.DimensionElements);
                foreach (int[] numArray2 in new ArrayIndexIterator(info.LowerBounds, info.UpperBounds, true))
                {
                    object obj3 = null;
                    if (category == DataTypeCategory.Primitive)
                    {
                        if (elementType != ((TcAdsDataType)adsType.BaseType).ManagedType)
                        {
                            throw new ArgumentException("Cannot convert data type of symbol to this type.", "type");
                        }
                        readerOffset += this.InitializePrimitiveType(adsType.BaseType.Name, elementType, adsType.DataTypeId, byteLength, reader, readerOffset, out obj3);
                        array.SetValue(obj3, numArray2);
                        continue;
                    }
                    if (category == DataTypeCategory.Enum)
                    {
                        if (elementType != ((TcAdsDataType)adsType.BaseType).ManagedType)
                        {
                            throw new ArgumentException("Cannot convert data type of symbol to this type.", "type");
                        }
                        readerOffset += this.InitializeEnum(adsType.BaseType.Name, elementType, adsType.DataTypeId, byteLength, reader, readerOffset, out obj3);
                        array.SetValue(obj3, numArray2);
                        continue;
                    }
                    if (adsType.BaseType.Category == DataTypeCategory.Struct)
                    {
                        readerOffset += this.InitializeStruct(adsType.BaseType.SubItems, elementType, reader, readerOffset, out obj3);
                        array.SetValue(obj3, numArray2);
                        continue;
                    }
                    if (adsType.BaseType.Category == DataTypeCategory.Array)
                    {
                        readerOffset += this.InitializeArray(elementType, (TcAdsDataType)adsType.BaseType, reader, readerOffset, jagLevel, out obj3);
                        array.SetValue(obj3, numArray2);
                    }
                }
            }
            value = array;
            return(readerOffset - num);
        }