Exemplo n.º 1
0
        private T[] CoerceArrayValue <T>(PropertyType unboxType)
        {
            if (this.Type == unboxType)
            {
                return((T[])this._data);
            }
            Array array = this._data as Array;

            if (array == null)
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", (object)this.Type, (object)typeof(T).MakeArrayType().Name), -2147316576);
            }
            PropertyType type = this.Type - 1024;

            T[] objArray = new T[array.Length];
            for (int index = 0; index < array.Length; ++index)
            {
                try
                {
                    objArray[index] = CLRIPropertyValueImpl.CoerceScalarValue <T>(type, array.GetValue(index));
                }
                catch (InvalidCastException ex)
                {
                    InvalidCastException invalidCastException = new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueArrayCoersion", (object)this.Type, (object)typeof(T).MakeArrayType().Name, (object)index, (object)ex.Message), (Exception)ex);
                    int hr = ex._HResult;
                    invalidCastException.SetErrorCode(hr);
                    throw invalidCastException;
                }
            }
            return(objArray);
        }
Exemplo n.º 2
0
        // Token: 0x0600640D RID: 25613 RVA: 0x00153A1C File Offset: 0x00151C1C
        private T[] CoerceArrayValue <T>(PropertyType unboxType)
        {
            if (this.Type == unboxType)
            {
                return((T[])this._data);
            }
            Array array = this._data as Array;

            if (array == null)
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", new object[]
                {
                    this.Type,
                    typeof(T).MakeArrayType().Name
                }), -2147316576);
            }
            PropertyType type = this.Type - 1024;

            T[] array2 = new T[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                try
                {
                    array2[i] = CLRIPropertyValueImpl.CoerceScalarValue <T>(type, array.GetValue(i));
                }
                catch (InvalidCastException ex)
                {
                    Exception ex2 = new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueArrayCoersion", new object[]
                    {
                        this.Type,
                        typeof(T).MakeArrayType().Name,
                        i,
                        ex.Message
                    }), ex);
                    ex2.SetErrorCode(ex._HResult);
                    throw ex2;
                }
            }
            return(array2);
        }
        private T[] CoerceArrayValue <T>(PropertyType unboxType)
        {
            // If we contain the type being looked for directly, then take the fast-path
            if (Type == unboxType)
            {
                return((T[])_data);
            }

            // Make sure we have an array to begin with
            Array dataArray = _data as Array;

            if (dataArray == null)
            {
                throw new InvalidCastException(SR.Format(SR.InvalidCast_WinRTIPropertyValueElement, this.Type, typeof(T).MakeArrayType().Name), __HResults.TYPE_E_TYPEMISMATCH);
            }

            // Array types are 1024 larger than their equivilent scalar counterpart
            BCLDebug.Assert((int)Type > 1024, "Unexpected array PropertyType value");
            PropertyType scalarType = Type - 1024;

            // If we do not have the correct array type, then we need to convert the array element-by-element
            // to a new array of the requested type
            T[] coercedArray = new T[dataArray.Length];
            for (int i = 0; i < dataArray.Length; ++i)
            {
                try
                {
                    coercedArray[i] = CoerceScalarValue <T>(scalarType, dataArray.GetValue(i));
                }
                catch (InvalidCastException elementCastException)
                {
                    Exception e = new InvalidCastException(SR.Format(SR.InvalidCast_WinRTIPropertyValueArrayCoersion, this.Type, typeof(T).MakeArrayType().Name, i, elementCastException.Message), elementCastException);
                    e.SetErrorCode(elementCastException._HResult);
                    throw e;
                }
            }

            return(coercedArray);
        }