示例#1
0
        private void checkCast <T>(object source, T value)
        {
            Assert.IsTrue(Typecasts.CanCastTo(source, typeof(T)));

            var result = Typecasts.CastTo(source, typeof(T));

            Assert.AreEqual(value, result);
        }
示例#2
0
        public void CastComplex()
        {
            checkCast <object>(complex, complex);

            Assert.IsTrue(Typecasts.CanCastTo(complex, typeof(IEnumerable <ITypedElement>)));
            var result = (IEnumerable <ITypedElement>)Typecasts.CastTo(complex, typeof(IEnumerable <ITypedElement>));

            Assert.AreEqual(complex, result.Single());
            checkCast <ITypedElement>(complex, complex);
            Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(bool)));
            Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(bool?)));
            Assert.IsFalse(Typecasts.CanCastTo(collection, typeof(string)));
        }
示例#3
0
        public void CastNullable()
        {
            checkCast <object>("hi", "hi");

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

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

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

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

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

            Assert.IsFalse(Typecasts.CanCastTo(4, typeof(string)));
            Assert.IsFalse(Typecasts.CanCastTo(4m, typeof(long?)));
        }
示例#4
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)));
        }