コード例 #1
0
        public static XunitSerializationTriple DeserializeTriple(string value)
        {
            var pieces = value.Split(new[] { ':' }, 3);

            if (pieces.Length < 2)
            {
                throw new ArgumentException("Data does not appear to be a valid serialized triple: " + value);
            }

            var pieceType         = SerializationHelper.GetType(pieces[1]);
            var deserializedValue = pieces.Length == 3 ? Deserialize(pieceType, pieces[2]) : null;

            return(new XunitSerializationTriple(pieces[0], deserializedValue, pieceType));
        }
コード例 #2
0
            public void Deserialize(IXunitSerializationInfo info)
            {
                var arrType     = SerializationHelper.GetType(info.GetValue <string>("ElementType"));
                var rank        = info.GetValue <int>("Rank");
                var totalLength = info.GetValue <int>("TotalLength");

                int[] lengths     = new int[rank];
                int[] lowerBounds = new int[rank];
                for (int i = 0; i < lengths.Length; i++)
                {
                    lengths[i]     = info.GetValue <int>("Length" + i);
                    lowerBounds[i] = info.GetValue <int>("LowerBound" + i);
                }

                array = Array.CreateInstance(arrType, lengths, lowerBounds);

                int[] indices = new int[rank];
                for (int i = 0; i < indices.Length; i++)
                {
                    indices[i] = lowerBounds[i];
                }
                for (int i = 0; i < totalLength; i++)
                {
                    bool complete = false;
                    for (int dim = rank - 1; dim >= 0; dim--)
                    {
                        if (indices[dim] >= lowerBounds[dim] + lengths[dim])
                        {
                            if (dim == 0)
                            {
                                complete = true;
                                break;
                            }
                            for (int j = dim; j < rank; j++)
                            {
                                indices[j] = lowerBounds[dim];
                            }
                            indices[dim - 1]++;
                        }
                    }
                    if (complete)
                    {
                        break;
                    }
                    object item = info.GetValue("Item" + i, arrType);
                    array.SetValue(item, indices);
                    indices[rank - 1]++;
                }
            }
コード例 #3
0
        public static object Deserialize(Type type, string serializedValue)
        {
            if (serializedValue == null)
            {
                return(null);
            }

            if (typeof(IXunitSerializable).IsAssignableFrom(type))
            {
                return(DeserializeSerializable(type, serializedValue));
            }

            if (type == typeof(char?) || type == typeof(char))
            {
                return((char)ushort.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(string))
            {
                return(FromBase64(serializedValue));
            }

            if (type == typeof(byte?) || type == typeof(byte))
            {
                return(byte.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(sbyte?) || type == typeof(sbyte))
            {
                return(sbyte.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(short?) || type == typeof(short))
            {
                return(short.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(ushort?) || type == typeof(ushort))
            {
                return(ushort.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(int?) || type == typeof(int))
            {
                return(int.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(uint?) || type == typeof(uint))
            {
                return(uint.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(long?) || type == typeof(long))
            {
                return(long.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(ulong?) || type == typeof(ulong))
            {
                return(ulong.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(float?) || type == typeof(float))
            {
                var    arrSer = (ArraySerializer)DeserializeSerializable(typeof(ArraySerializer), serializedValue);
                byte[] bytes  = (byte[])arrSer.ArrayData;
                return(BitConverter.ToSingle(bytes, 0));
            }

            if (type == typeof(double?) || type == typeof(double))
            {
                var    arrSer = (ArraySerializer)DeserializeSerializable(typeof(ArraySerializer), serializedValue);
                byte[] bytes  = (byte[])arrSer.ArrayData;
                return(BitConverter.ToDouble(bytes, 0));
            }

            if (type == typeof(decimal?) || type == typeof(decimal))
            {
                return(decimal.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(bool?) || type == typeof(bool))
            {
                return(bool.Parse(serializedValue));
            }

            if (type == typeof(DateTime?) || type == typeof(DateTime))
            {
                var styles = serializedValue.EndsWith("Z", StringComparison.Ordinal) ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.None;
                return(DateTime.Parse(serializedValue, CultureInfo.InvariantCulture, styles));
            }

            if (type == typeof(DateTimeOffset?) || type == typeof(DateTimeOffset))
            {
                var styles = serializedValue.EndsWith("Z", StringComparison.Ordinal) ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.None;
                return(DateTimeOffset.Parse(serializedValue, CultureInfo.InvariantCulture, styles));
            }

            if (type == typeof(Type))
            {
                return(SerializationHelper.GetType(serializedValue));
            }

            if (type.IsEnum() || type.IsNullableEnum())
            {
                return(Enum.Parse(type.UnwrapNullable(), serializedValue));
            }

            if (type.IsArray)
            {
                var arrSer = (ArraySerializer)DeserializeSerializable(typeof(ArraySerializer), serializedValue);
                return(arrSer.ArrayData);
            }

            throw new ArgumentException("We don't know how to de-serialize type " + type.FullName, nameof(serializedValue));
        }