コード例 #1
0
        public void FindCastToWorks()
        {
            var t = typeof(Cast1);

            Assert.Equal(typeof(int), MethodInfoEx.FindCastToOperator(t, typeof(int)).ReturnType);
            Assert.Equal(typeof(double), MethodInfoEx.FindCastToOperator(t, typeof(double)).ReturnType);
            Assert.Equal(typeof(Cast2), MethodInfoEx.FindCastToOperator(t, typeof(Cast2)).ReturnType);
            Assert.Null(MethodInfoEx.FindCastToOperator(t, typeof(string)));
            Assert.Null(MethodInfoEx.FindCastToOperator(t, typeof(decimal)));
        }
コード例 #2
0
        public void FindCastWorks()
        {
            var t1 = typeof(Cast1);
            var t2 = typeof(Cast2);

            Assert.Equal(typeof(int), MethodInfoEx.FindCastOperator(t1, typeof(int)).ReturnType);
            Assert.Equal(typeof(double), MethodInfoEx.FindCastOperator(t1, typeof(double)).ReturnType);
            Assert.Null(MethodInfoEx.FindCastToOperator(t1, typeof(string)));
            Assert.Null(MethodInfoEx.FindCastToOperator(t1, typeof(decimal)));

            Assert.Equal(typeof(int), MethodInfoEx.FindCastOperator(typeof(int), t2).GetParameterTypes()[0]);
            Assert.Equal(typeof(double), MethodInfoEx.FindCastOperator(typeof(double), t2).GetParameterTypes()[0]);
            Assert.Equal(t2, MethodInfoEx.FindCastOperator(t2, t1).GetParameterTypes()[0]);
            Assert.Null(MethodInfoEx.FindCastOperator(typeof(string), t2));
            Assert.Null(MethodInfoEx.FindCastOperator(typeof(decimal), t2));

            Assert.Throws <AmbiguousMatchException>(() => MethodInfoEx.FindCastOperator(t1, t2));
        }