Esempio n. 1
0
        public void CanConvertFrom()
        {
            EnumConverter converter = new EnumConverter(typeof(E));

            Assert.IsTrue(converter.CanConvertFrom(typeof(string)), "#A1");
            Assert.IsFalse(converter.CanConvertFrom(typeof(Enum)), "#A2");
            Assert.IsFalse(converter.CanConvertFrom(typeof(object)), "#A3");
            Assert.IsFalse(converter.CanConvertFrom(typeof(int)), "#A4");
            Assert.IsTrue(converter.CanConvertFrom(typeof(InstanceDescriptor)), "#A5");
            Assert.IsFalse(converter.CanConvertFrom(typeof(string [])), "#A6");
#if NET_2_0
            Assert.IsTrue(converter.CanConvertFrom(typeof(Enum [])), "#A7");
#else
            Assert.IsFalse(converter.CanConvertFrom(typeof(Enum [])), "#A7");
#endif

            converter = new EnumConverter(typeof(E2));
            Assert.IsTrue(converter.CanConvertFrom(typeof(string)), "#B1");
            Assert.IsFalse(converter.CanConvertFrom(typeof(Enum)), "#B2");
            Assert.IsFalse(converter.CanConvertFrom(typeof(object)), "#B3");
            Assert.IsFalse(converter.CanConvertFrom(typeof(int)), "#B4");
            Assert.IsTrue(converter.CanConvertFrom(typeof(InstanceDescriptor)), "#B5");
            Assert.IsFalse(converter.CanConvertFrom(typeof(string [])), "#B6");
#if NET_2_0
            Assert.IsTrue(converter.CanConvertFrom(typeof(Enum [])), "#B7");
#else
            Assert.IsFalse(converter.CanConvertFrom(typeof(Enum [])), "#B7");
#endif
        }
Esempio n. 2
0
        /// <summary>
        /// Helper method for converting from a string to an enum using a converter.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="culture">The culture to use to read the localized strings</param>
        /// <returns></returns>
        public static Nullable <T> ConvertFromType <T>(string value, CultureInfo culture)
            where T : struct
        {
            Nullable <T> returnValue = new Nullable <T>();

            returnValue = returnValue.GetValueOrDefault();

            if (value == null)
            {
                return(returnValue);
            }

            EnumConverter converter = GetEnumConverter <T>();

            if (converter == null)
            {
                return(returnValue);
            }

            if (converter.CanConvertFrom(value.GetType()))
            {
                object converted = converter.ConvertFrom(null, culture, value);

                if (converted != null && (converted is T))
                {
                    returnValue = (T)converted;
                }
            }

            return(returnValue);
        }
Esempio n. 3
0
        public static void CanConvertFromInt32()
        {
            // Arrange
            var converter = new EnumConverter(typeof(Answer));

            // Act
            var result = converter.CanConvertFrom(typeof(int));

            // Assert
            Assert.True(result);
        }
Esempio n. 4
0
 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 {
     return(_enumConverter.CanConvertFrom(context, sourceType));
 }
Esempio n. 5
0
        public void CanConvertFrom()
        {
            var converter = new EnumConverter(typeof(TestEnum));

            Assert.IsTrue(converter.CanConvertFrom(typeof(string)));
        }