示例#1
0
        public void CastNullable()
        {
            checkCast <object>("hi", "hi");

            Assert.True(Typecasts.CanCastTo("hi", typeof(IEnumerable <ITypedElement>)));
            var result = (IEnumerable <ITypedElement>)Typecasts.CastTo("hi", typeof(IEnumerable <ITypedElement>));

            Assert.Equal("hi", result.Single().Value);

            Assert.True(Typecasts.CanCastTo("hi", typeof(ITypedElement)));
            var result2 = (ITypedElement)Typecasts.CastTo("hi", typeof(ITypedElement));

            Assert.Equal("hi", result2.Value);

            checkCast <bool?>(true, true);
            checkCast <decimal?>(4L, 4m);
            checkCast <string>("hi", "hi");

            Assert.False(Typecasts.CanCastTo(4, typeof(string)));
            Assert.False(Typecasts.CanCastTo(4m, typeof(long?)));
        }
示例#2
0
        public void CastValue()
        {
            checkCast <object>(4L, 4L);

            Assert.True(Typecasts.CanCastTo(4, typeof(IEnumerable <IElementNavigator>)));
            var result = (IEnumerable <IElementNavigator>)Typecasts.CastTo(4L, typeof(IEnumerable <IElementNavigator>));

            Assert.Equal(4L, result.Single().Value);

            Assert.True(Typecasts.CanCastTo(4L, typeof(IElementNavigator)));
            var result2 = (IElementNavigator)Typecasts.CastTo(4L, typeof(IElementNavigator));

            Assert.Equal(4L, result2.Value);

            checkCast <bool>(true, true);
            checkCast <decimal>(4L, 4m);

            checkCast <bool?>(true, true);
            checkCast <decimal?>(4L, 4m);
            checkCast <string>("hi", "hi");

            Assert.False(Typecasts.CanCastTo(4, typeof(string)));
            Assert.False(Typecasts.CanCastTo(4m, typeof(long)));
        }