public static void TestMetaObject(DynamicMetaObject target, Type type, bool isValid = true)
            {
                ConvertBinder     binder = new TestConvertBinder(type);
                DynamicMetaObject result = target.BindConvert(binder);

                Assert.IsNotNull(result);

                // Convert expression
                UnaryExpression expression = result.Expression as UnaryExpression;

                Assert.IsNotNull(expression);
                Assert.AreEqual <Type>(binder.Type, expression.Type);

                if (isValid)
                {
                    MethodCallExpression methodCallExp = expression.Operand as MethodCallExpression;

                    if (methodCallExp != null)
                    {
                        Assert.IsTrue(methodCallExp.Method.ToString().Contains("CastValue"));
                    }
                    else
                    {
                        ParameterExpression paramExpression = expression.Operand as ParameterExpression;
                        Assert.IsNotNull(paramExpression);
                    }
                }
                else
                {
                    Expression <Action> throwExp = Expression.Lambda <Action>(Expression.Block(expression), new ParameterExpression[] { });
                    ExceptionTestHelper.ExpectException <InvalidCastException>(() => throwExp.Compile().Invoke());
                }
            }
        public void BindConvertTest()
        {
            JsonValue         value;
            DynamicMetaObject target;

            value  = (JsonValue)AnyInstance.AnyInt;
            target = GetJsonValueDynamicMetaObject(value);
            TestConvertBinder.TestBindParams(target);

            Type[] intTypes = { typeof(int), typeof(uint), typeof(long), };

            foreach (Type type in intTypes)
            {
                TestConvertBinder.TestMetaObject(target, type);
            }

            value  = (JsonValue)AnyInstance.AnyString;
            target = GetJsonValueDynamicMetaObject(value);
            TestConvertBinder.TestMetaObject(target, typeof(string));

            value  = (JsonValue)AnyInstance.AnyJsonValue1;
            target = GetJsonValueDynamicMetaObject(value);
            TestConvertBinder.TestMetaObject(target, typeof(JsonValue));
            TestConvertBinder.TestMetaObject(target, typeof(IEnumerable <KeyValuePair <string, JsonValue> >));
            TestConvertBinder.TestMetaObject(target, typeof(IDynamicMetaObjectProvider));
            TestConvertBinder.TestMetaObject(target, typeof(object));

            TestConvertBinder.TestMetaObject(target, typeof(Person), false);
        }
            public static void TestMetaObject(DynamicMetaObject target, Type type, bool isValid = true)
            {
                ConvertBinder binder = new TestConvertBinder(type);
                DynamicMetaObject result = target.BindConvert(binder);
                Assert.IsNotNull(result);

                // Convert expression
                UnaryExpression expression = result.Expression as UnaryExpression;
                Assert.IsNotNull(expression);
                Assert.AreEqual<Type>(binder.Type, expression.Type);

                if (isValid)
                {
                    MethodCallExpression methodCallExp = expression.Operand as MethodCallExpression;

                    if (methodCallExp != null)
                    {
                        Assert.IsTrue(methodCallExp.Method.ToString().Contains("CastValue"));
                    }
                    else
                    {
                        ParameterExpression paramExpression = expression.Operand as ParameterExpression;
                        Assert.IsNotNull(paramExpression);
                    }
                }
                else
                {
                    Expression<Action> throwExp = Expression.Lambda<Action>(Expression.Block(expression), new ParameterExpression[] { });
                    ExceptionTestHelper.ExpectException<InvalidCastException>(() => throwExp.Compile().Invoke());
                }
            }
 public static void TestBindParams(DynamicMetaObject target)
 {
     ConvertBinder binder = new TestConvertBinder(typeof(int));
     ExceptionTestHelper.ExpectException<ArgumentNullException>(() => { var result = target.BindConvert(null); });
 }
            public static void TestBindParams(DynamicMetaObject target)
            {
                ConvertBinder binder = new TestConvertBinder(typeof(int));

                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindConvert(null); });
            }