示例#1
0
        public object Deserialize(CefValue value, Type targetType, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(value, targetType))
            {
                throw new InvalidOperationException();
            }

            object result = null;
            var    type   = value.GetValueType();

            switch (type)
            {
            case CefValueType.Bool:
                if (targetType == typeof(bool) || targetType == typeof(object))
                {
                    result = value.GetBool();
                }
                break;

            case CefValueType.Binary:
                if (value.IsType(CefTypes.Int64) && (targetType == typeof(object) || targetType == typeof(long)))
                {
                    result = value.GetInt64();
                }
                break;

            case CefValueType.Int:
                var intVal = value.GetInt();
                if (targetType == typeof(byte))
                {
                    result = Convert.ToByte(intVal);
                }
                else if (targetType == typeof(sbyte))
                {
                    result = Convert.ToSByte(intVal);
                }
                else if (targetType == typeof(short))
                {
                    result = Convert.ToInt16(intVal);
                }
                else if (targetType == typeof(ushort))
                {
                    result = Convert.ToUInt16(intVal);
                }
                else if (targetType == typeof(int) || targetType == typeof(object))
                {
                    result = intVal;
                }
                else if (targetType == typeof(uint))
                {
                    result = Convert.ToUInt32(intVal);
                }
                else if (targetType == typeof(long))
                {
                    result = Convert.ToInt64(intVal);
                }
                else if (targetType == typeof(ulong))
                {
                    result = Convert.ToUInt64(intVal);
                }
                else if (targetType == typeof(double))
                {
                    result = Convert.ToDouble(intVal);
                }
                else if (targetType == typeof(float))
                {
                    result = Convert.ToSingle(intVal);
                }
                break;

            case CefValueType.Double:
                var doubleVal = value.GetDouble();
                if (targetType == typeof(byte))
                {
                    result = Convert.ToByte(doubleVal);
                }
                else if (targetType == typeof(sbyte))
                {
                    result = Convert.ToSByte(doubleVal);
                }
                else if (targetType == typeof(short))
                {
                    result = Convert.ToInt16(doubleVal);
                }
                else if (targetType == typeof(ushort))
                {
                    result = Convert.ToUInt16(doubleVal);
                }
                else if (targetType == typeof(int))
                {
                    result = Convert.ToInt32(doubleVal);
                }
                else if (targetType == typeof(uint))
                {
                    result = Convert.ToUInt32(doubleVal);
                }
                else if (targetType == typeof(long))
                {
                    result = Convert.ToInt64(doubleVal);
                }
                else if (targetType == typeof(ulong))
                {
                    result = Convert.ToUInt64(doubleVal);
                }
                else if (targetType == typeof(double) || targetType == typeof(object))
                {
                    result = doubleVal;
                }
                else if (targetType == typeof(float))
                {
                    result = Convert.ToSingle(doubleVal);
                }
                break;

            case CefValueType.String:
                var strVal = value.GetString();
                if (targetType == typeof(char) && !string.IsNullOrEmpty(strVal))
                {
                    result = strVal.First();
                }
                break;
            }

            return(result);
        }