public static object Read(this IDictionary <string, string> data, Type type)
        {
            var t            = Activator.CreateInstance(type);
            var propertyList =
                type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanWrite);

            foreach (var propertyInfo in propertyList)
            {
                string textValue;
                if (!data.TryGetValue(propertyInfo.Name, out textValue))
                {
                    continue;
                }

                var typedValue = MvxStringToTypeParser.ReadValue(textValue, propertyInfo.PropertyType, propertyInfo.Name);
                propertyInfo.SetValue(t, typedValue, new object[0]);
            }

            return(t);
        }
        public static IEnumerable <object> CreateArgumentList(this IDictionary <string, string> data, IEnumerable <ParameterInfo> requiredParameters, string debugText)
        {
            var argumentList = new List <object>();

            foreach (var requiredParameter in requiredParameters)
            {
                string parameterValue;
                if (data == null ||
                    !data.TryGetValue(requiredParameter.Name, out parameterValue))
                {
                    MvxTrace.Trace(
                        "Missing parameter for call to {0} - missing parameter {1} - asssuming null - this may fail for value types!",
                        debugText,
                        requiredParameter.Name);
                    parameterValue = null;
                }

                var value = MvxStringToTypeParser.ReadValue(parameterValue, requiredParameter.ParameterType,
                                                            requiredParameter.Name);
                argumentList.Add(value);
            }
            return(argumentList);
        }
        public void Test_AllTypesCanBeRead()
        {
            var parser = new MvxStringToTypeParser();
            Assert.AreEqual("Hello World", parser.ReadValue("Hello World", typeof(string), "ignored"));
            Assert.AreEqual("", parser.ReadValue("", typeof(string), "ignored"));
            Assert.AreEqual(null, parser.ReadValue(null, typeof(string), "ignored"));

            Assert.AreEqual(1.23, parser.ReadValue("1.23", typeof(double), "ignored"));
            Assert.AreEqual(123.0, parser.ReadValue("1,23", typeof(double), "ignored"),
                            "comma separators ignored under invariant parsing");
            Assert.AreEqual(0, parser.ReadValue("garbage", typeof(double), "ignored"));
            Assert.AreEqual(0, parser.ReadValue("", typeof(double), "ignored"));
            Assert.AreEqual(0, parser.ReadValue(null, typeof(double), "ignored"));

            Assert.AreEqual(1.23f, parser.ReadValue("1.23", typeof(float), "ignored"));
            Assert.AreEqual(123.0f, parser.ReadValue("1,23", typeof(float), "ignored"),
                            "comma separators ignored under invariant parsing");
            Assert.AreEqual(0f, parser.ReadValue("garbage", typeof(float), "ignored"));
            Assert.AreEqual(0f, parser.ReadValue("", typeof(float), "ignored"));
            Assert.AreEqual(0f, parser.ReadValue(null, typeof(float), "ignored"));

            Assert.AreEqual(123, parser.ReadValue("123", typeof(int), "ignored"));
            Assert.AreEqual(0, parser.ReadValue("12.3", typeof(int), "ignored"),
                            "partial integers should not be parsed");
            Assert.AreEqual(0, parser.ReadValue("garbage", typeof(int), "ignored"));
            Assert.AreEqual(0, parser.ReadValue("", typeof(int), "ignored"));
            Assert.AreEqual(0, parser.ReadValue(null, typeof(int), "ignored"));

            Assert.AreEqual(123L, parser.ReadValue("123", typeof(long), "ignored"));
            Assert.AreEqual(0, parser.ReadValue("12.3", typeof(long), "ignored"),
                            "partial integers should not be parsed");
            Assert.AreEqual(0L, parser.ReadValue("garbage", typeof(long), "ignored"));
            Assert.AreEqual(0L, parser.ReadValue("", typeof(long), "ignored"));
            Assert.AreEqual(0L, parser.ReadValue(null, typeof(long), "ignored"));

            Assert.AreEqual((ulong)123L, parser.ReadValue("123", typeof(ulong), "ignored"));
            Assert.AreEqual((ulong)0, parser.ReadValue("12.3", typeof(ulong), "ignored"),
                            "partial integers should not be parsed");
            Assert.AreEqual((ulong)0L, parser.ReadValue("garbage", typeof(ulong), "ignored"));
            Assert.AreEqual((ulong)0L, parser.ReadValue("", typeof(ulong), "ignored"));
            Assert.AreEqual((ulong)0L, parser.ReadValue(null, typeof(ulong), "ignored"));

            Assert.AreEqual((ushort)123, parser.ReadValue("123", typeof(ushort), "ignored"));
            Assert.AreEqual((ushort)0, parser.ReadValue("12.3", typeof(ushort), "ignored"),
                            "partial integers should not be parsed");
            Assert.AreEqual((ushort)0, parser.ReadValue("garbage", typeof(ushort), "ignored"));
            Assert.AreEqual((ushort)0, parser.ReadValue("", typeof(ushort), "ignored"));
            Assert.AreEqual((ushort)0, parser.ReadValue(null, typeof(ushort), "ignored"));

            Assert.AreEqual(true, parser.ReadValue("true", typeof(bool), "ignored"));
            Assert.AreEqual(true, parser.ReadValue("True", typeof(bool), "ignored"));
            Assert.AreEqual(true, parser.ReadValue("TRUE", typeof(bool), "ignored"));
            Assert.AreEqual(true, parser.ReadValue("truE", typeof(bool), "ignored"));
            Assert.AreEqual(false, parser.ReadValue("false", typeof(bool), "ignored"));
            Assert.AreEqual(false, parser.ReadValue("False", typeof(bool), "ignored"));
            Assert.AreEqual(false, parser.ReadValue("FALSE", typeof(bool), "ignored"));
            Assert.AreEqual(false, parser.ReadValue("falsE", typeof(bool), "ignored"));
            Assert.AreEqual(false, parser.ReadValue("garbage", typeof(bool), "ignored"));
            Assert.AreEqual(false, parser.ReadValue("", typeof(bool), "ignored"));
            Assert.AreEqual(false, parser.ReadValue(null, typeof(bool), "ignored"));

            var guid = Guid.Parse("{C3CF9078-0122-41BD-9E2D-D3199E937285}");
            Assert.AreEqual(guid,
                            parser.ReadValue("{C3CF9078-0122-41BD-9E2D-D3199E937285}", typeof(Guid),
                                             "ignored"));
            Assert.AreEqual(guid,
                            parser.ReadValue(
                                "{C3CF9078-0122-41BD-9E2D-D3199E937285}".ToLowerInvariant(), typeof(Guid), "ignored"));
            Assert.AreEqual(Guid.Empty,
                            parser.ReadValue("{F9078-0122-41BD-9E2D-D3199E93}", typeof(Guid), "ignored"));
            Assert.AreEqual(Guid.Empty, parser.ReadValue("garbage", typeof(Guid), "ignored"));
            Assert.AreEqual(Guid.Empty, parser.ReadValue("", typeof(Guid), "ignored"));
            Assert.AreEqual(Guid.Empty, parser.ReadValue(null, typeof(Guid), "ignored"));

            Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries,
                            parser.ReadValue("RemoveEmptyEntries", typeof(StringSplitOptions), "ignored"));
            Assert.AreEqual(StringSplitOptions.None,
                            parser.ReadValue("None".ToLowerInvariant(), typeof(StringSplitOptions),
                                             "ignored"));
            Assert.AreEqual(StringSplitOptions.None,
                            parser.ReadValue("garbage", typeof(StringSplitOptions), "ignored"));
            Assert.AreEqual(StringSplitOptions.None,
                            parser.ReadValue("", typeof(StringSplitOptions), "ignored"));
            Assert.AreEqual(StringSplitOptions.None,
                            parser.ReadValue(null, typeof(StringSplitOptions), "ignored"));
        }