Ejemplo n.º 1
0
        public void CanConvertTo()
        {
            EnumConverter converter = new EnumConverter(typeof(E));

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

            converter = new EnumConverter(typeof(E2));
            Assert.IsTrue(converter.CanConvertTo(typeof(string)), "#B1");
            Assert.IsFalse(converter.CanConvertTo(typeof(Enum)), "#B2");
            Assert.IsFalse(converter.CanConvertTo(typeof(object)), "#B3");
            Assert.IsFalse(converter.CanConvertTo(typeof(object)), "#B4");
            Assert.IsTrue(converter.CanConvertTo(typeof(InstanceDescriptor)), "#B5");
            Assert.IsFalse(converter.CanConvertTo(typeof(string [])), "#B6");
#if NET_2_0
            Assert.IsTrue(converter.CanConvertTo(typeof(Enum [])), "#B7");
#else
            Assert.IsFalse(converter.CanConvertTo(typeof(Enum [])), "#B7");
#endif
        }
Ejemplo n.º 2
0
        public static void CanConvertToInt32()
        {
            // Arrange
            var converter = new EnumConverter(typeof(Answer));

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

            // Assert
            Assert.True(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Helper method to call a converter explicitely to convert to an enum type
        /// </summary>
        /// <typeparam name="T">THe enum to convert to</typeparam>
        /// <param name="value">The enum value to be converted to</param>
        /// <param name="typeToConvert">The type to convert</param>
        /// <param name="culture">The culture to use to read the localized strings</param>
        /// <returns></returns>
        public static object ConvertToType<T>(T value, Type typeToConvert, CultureInfo culture)
            where T : struct
        {
            EnumConverter converter = GetEnumConverter<T>();
            if (converter == null)
            {
                return null;
            }
            if (converter.CanConvertTo(typeToConvert))
            {
                return converter.ConvertTo(null, culture, value, typeToConvert);
            }

            return null;
        }
Ejemplo n.º 4
0
 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
 {
     return(_enumConverter.CanConvertTo(destinationType) || base.CanConvertTo(context, destinationType));
 }