internal static object GetWellKnownScalar(IPropertyValue ipv, PropertyType type)
        {
            switch (type)
            {
            case PropertyType.UInt8:
                return(ipv.GetUInt8());

            case PropertyType.Int16:
                return(ipv.GetInt16());

            case PropertyType.UInt16:
                return(ipv.GetUInt16());

            case PropertyType.Int32:
                return(ipv.GetInt32());

            case PropertyType.UInt32:
                return(ipv.GetUInt32());

            case PropertyType.Int64:
                return(ipv.GetInt64());

            case PropertyType.UInt64:
                return(ipv.GetUInt64());

            case PropertyType.Single:
                return(ipv.GetSingle());

            case PropertyType.Double:
                return(ipv.GetDouble());
            }

            Debug.Assert(false);
            return(null);
        }
示例#2
0
        private static T CoerceScalarValue <T>(PropertyType type, object value)
        {
            // If the property type is neither one of the coercable numeric types nor IInspectable, we
            // should not attempt coersion, even if the underlying value is technically convertable
            if (!IsCoercable(type, value) && type != PropertyType.Inspectable)
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", type, typeof(T).Name), __HResults.TYPE_E_TYPEMISMATCH);
            }

            try {
                // Try to coerce:
                //  * String <--> Guid
                //  * Numeric scalars
                if (type == PropertyType.String && typeof(T) == typeof(Guid))
                {
                    return((T)(object)Guid.Parse((string)value));
                }
                else if (type == PropertyType.Guid && typeof(T) == typeof(String))
                {
                    return((T)(object)((Guid)value).ToString("D", System.Globalization.CultureInfo.InvariantCulture));
                }
                else
                {
                    // Iterate over the numeric scalars, to see if we have a match for one of the known conversions
                    foreach (Tuple <Type, PropertyType> numericScalar in NumericScalarTypes)
                    {
                        if (numericScalar.Item1 == typeof(T))
                        {
                            return((T)Convert.ChangeType(value, typeof(T), System.Globalization.CultureInfo.InvariantCulture));
                        }
                    }
                }
            }
            catch (FormatException) {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", type, typeof(T).Name), __HResults.TYPE_E_TYPEMISMATCH);
            }
            catch (InvalidCastException) {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", type, typeof(T).Name), __HResults.TYPE_E_TYPEMISMATCH);
            }
            catch (OverflowException) {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueCoersion", type, value, typeof(T).Name), __HResults.DISP_E_OVERFLOW);
            }

            // If the property type is IInspectable, and we have a nested IPropertyValue, then we need
            // to pass along the request to coerce the value.
            IPropertyValue ipv = value as IPropertyValue;

            if (type == PropertyType.Inspectable && ipv != null)
            {
                if (typeof(T) == typeof(Byte))
                {
                    return((T)(object)ipv.GetUInt8());
                }
                else if (typeof(T) == typeof(Int16))
                {
                    return((T)(object)ipv.GetInt16());
                }
                else if (typeof(T) == typeof(UInt16))
                {
                    return((T)(object)ipv.GetUInt16());
                }
                else if (typeof(T) == typeof(Int32))
                {
                    return((T)(object)ipv.GetUInt32());
                }
                else if (typeof(T) == typeof(UInt32))
                {
                    return((T)(object)ipv.GetUInt32());
                }
                else if (typeof(T) == typeof(Int64))
                {
                    return((T)(object)ipv.GetInt64());
                }
                else if (typeof(T) == typeof(UInt64))
                {
                    return((T)(object)ipv.GetUInt64());
                }
                else if (typeof(T) == typeof(Single))
                {
                    return((T)(object)ipv.GetSingle());
                }
                else if (typeof(T) == typeof(Double))
                {
                    return((T)(object)ipv.GetDouble());
                }
                else
                {
                    BCLDebug.Assert(false, "T in coersion function wasn't understood as a type that can be coerced - make sure that CoerceScalarValue and NumericScalarTypes are in sync");
                }
            }

            // Otherwise, this is an invalid coersion
            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", type, typeof(T).Name), __HResults.TYPE_E_TYPEMISMATCH);
        }
示例#3
0
        // Token: 0x0600640F RID: 25615 RVA: 0x00153B70 File Offset: 0x00151D70
        private static T CoerceScalarValue <T>(PropertyType type, object value)
        {
            if (!CLRIPropertyValueImpl.IsCoercable(type, value) && type != PropertyType.Inspectable)
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", new object[]
                {
                    type,
                    typeof(T).Name
                }), -2147316576);
            }
            try
            {
                if (type == PropertyType.String && typeof(T) == typeof(Guid))
                {
                    return((T)((object)Guid.Parse((string)value)));
                }
                if (type == PropertyType.Guid && typeof(T) == typeof(string))
                {
                    return((T)((object)((Guid)value).ToString("D", CultureInfo.InvariantCulture)));
                }
                foreach (Tuple <Type, PropertyType> tuple in CLRIPropertyValueImpl.NumericScalarTypes)
                {
                    if (tuple.Item1 == typeof(T))
                    {
                        return((T)((object)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture)));
                    }
                }
            }
            catch (FormatException)
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", new object[]
                {
                    type,
                    typeof(T).Name
                }), -2147316576);
            }
            catch (InvalidCastException)
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", new object[]
                {
                    type,
                    typeof(T).Name
                }), -2147316576);
            }
            catch (OverflowException)
            {
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueCoersion", new object[]
                {
                    type,
                    value,
                    typeof(T).Name
                }), -2147352566);
            }
            IPropertyValue propertyValue = value as IPropertyValue;

            if (type == PropertyType.Inspectable && propertyValue != null)
            {
                if (typeof(T) == typeof(byte))
                {
                    return((T)((object)propertyValue.GetUInt8()));
                }
                if (typeof(T) == typeof(short))
                {
                    return((T)((object)propertyValue.GetInt16()));
                }
                if (typeof(T) == typeof(ushort))
                {
                    return((T)((object)propertyValue.GetUInt16()));
                }
                if (typeof(T) == typeof(int))
                {
                    return((T)((object)propertyValue.GetUInt32()));
                }
                if (typeof(T) == typeof(uint))
                {
                    return((T)((object)propertyValue.GetUInt32()));
                }
                if (typeof(T) == typeof(long))
                {
                    return((T)((object)propertyValue.GetInt64()));
                }
                if (typeof(T) == typeof(ulong))
                {
                    return((T)((object)propertyValue.GetUInt64()));
                }
                if (typeof(T) == typeof(float))
                {
                    return((T)((object)propertyValue.GetSingle()));
                }
                if (typeof(T) == typeof(double))
                {
                    return((T)((object)propertyValue.GetDouble()));
                }
            }
            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_WinRTIPropertyValueElement", new object[]
            {
                type,
                typeof(T).Name
            }), -2147316576);
        }