Exemplo n.º 1
0
        public void ConvertToStringTest()
        {
            var converter = new EnumConverter(typeof(TestEnum));

            Assert.AreEqual("None", converter.ConvertToString((TestEnum)0));
            Assert.AreEqual("None", converter.ConvertToString(TestEnum.None));
            Assert.AreEqual("One", converter.ConvertToString((TestEnum)1));
            Assert.AreEqual("One", converter.ConvertToString(TestEnum.One));
            Assert.AreEqual("", converter.ConvertToString(null));
        }
Exemplo n.º 2
0
        public void ConvertToStringTest()
        {
            var converter = new EnumConverter(typeof(TestEnum));

            Assert.AreEqual("None", converter.ConvertToString(CultureInfo.CurrentCulture, (TestEnum)0));
            Assert.AreEqual("None", converter.ConvertToString(CultureInfo.CurrentCulture, TestEnum.None));
            Assert.AreEqual("One", converter.ConvertToString(CultureInfo.CurrentCulture, (TestEnum)1));
            Assert.AreEqual("One", converter.ConvertToString(CultureInfo.CurrentCulture, TestEnum.One));
            Assert.AreEqual("", converter.ConvertToString(CultureInfo.CurrentCulture, null));
        }
Exemplo n.º 3
0
        public void ConvertToStringTest()
        {
            var converter       = new EnumConverter(typeof(TestEnum));
            var propertyMapData = new MemberMapData(null);

            Assert.Equal("None", converter.ConvertToString((TestEnum)0, null, propertyMapData));
            Assert.Equal("None", converter.ConvertToString(TestEnum.None, null, propertyMapData));
            Assert.Equal("One", converter.ConvertToString((TestEnum)1, null, propertyMapData));
            Assert.Equal("One", converter.ConvertToString(TestEnum.One, null, propertyMapData));
            Assert.Equal("", converter.ConvertToString(null, null, propertyMapData));
        }
Exemplo n.º 4
0
        public void ConvertToStringTest()
        {
            var converter            = new EnumConverter(typeof(TestEnum));
            var typeConverterOptions = new TypeConverterOptions
            {
                CultureInfo = CultureInfo.CurrentCulture
            };

            Assert.AreEqual("None", converter.ConvertToString(typeConverterOptions, (TestEnum)0));
            Assert.AreEqual("None", converter.ConvertToString(typeConverterOptions, TestEnum.None));
            Assert.AreEqual("One", converter.ConvertToString(typeConverterOptions, (TestEnum)1));
            Assert.AreEqual("One", converter.ConvertToString(typeConverterOptions, TestEnum.One));
            Assert.AreEqual("", converter.ConvertToString(typeConverterOptions, null));
        }
Exemplo n.º 5
0
        public void ConvertToStringTest()
        {
            var converter       = new EnumConverter(typeof(TestEnum));
            var propertyMapData = new MemberMapData(null)
            {
                TypeConverter        = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            Assert.AreEqual("None", converter.ConvertToString((TestEnum)0, null, propertyMapData));
            Assert.AreEqual("None", converter.ConvertToString(TestEnum.None, null, propertyMapData));
            Assert.AreEqual("One", converter.ConvertToString((TestEnum)1, null, propertyMapData));
            Assert.AreEqual("One", converter.ConvertToString(TestEnum.One, null, propertyMapData));
            Assert.AreEqual("", converter.ConvertToString(null, null, propertyMapData));
        }
        private static string AnimationCurveToString(object value)
        {
            if (value == null)
            {
                return(string.Empty);
            }
            AnimationCurve animationCurve = (AnimationCurve)value;
            StringBuilder  stringBuilder  = new StringBuilder();
            EnumConverter  enumConverter  = new EnumConverter(typeof(WrapMode));

            stringBuilder.AppendFormat("{0}\n{1}", enumConverter.ConvertToString(animationCurve.postWrapMode), enumConverter.ConvertToString(animationCurve.preWrapMode));
            Keyframe[] keys = animationCurve.keys;
            for (int i = 0; i < keys.Length; i++)
            {
                Keyframe keyframe = keys[i];
                stringBuilder.AppendFormat("\n{0}, {1}, {2}, {3}", new object[]
                {
                    AnimationCurveTypeConverter.InvStr(keyframe.inTangent),
                    AnimationCurveTypeConverter.InvStr(keyframe.outTangent),
                    AnimationCurveTypeConverter.InvStr(keyframe.time),
                    AnimationCurveTypeConverter.InvStr(keyframe.value)
                });
            }
            return(stringBuilder.ToString());
        }
Exemplo n.º 7
0
        public void ConvertToString_NameAttribute_ReturnsNameFromNameAttribute()
        {
            var converter       = new EnumConverter(typeof(NameAttributeEnum));
            var propertyMapData = new MemberMapData(null)
            {
                TypeConverter        = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            var value = converter.ConvertToString(NameAttributeEnum.Foo, null, propertyMapData);

            Assert.AreEqual("Bar", value);
        }
Exemplo n.º 8
0
        public void ConvertToString_Int16Enum_ReturnsString()
        {
            var converter = new EnumConverter(typeof(Int16Enum));

            var propertyMapData = new MemberMapData(null)
            {
                TypeConverter        = converter,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            var value = converter.ConvertToString(Int16Enum.One, null, propertyMapData);

            Assert.AreEqual("One", value);
        }
Exemplo n.º 9
0
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            CodeExpression left = null;

            using (CodeDomSerializerBase.TraceScope("EnumCodeDomSerializer::Serialize"))
            {
                Enum[] enumArray;
                if (!(value is Enum))
                {
                    return(left);
                }
                bool          flag      = false;
                TypeConverter converter = TypeDescriptor.GetConverter(value);
                if ((converter != null) && converter.CanConvertTo(typeof(Enum[])))
                {
                    enumArray = (Enum[])converter.ConvertTo(value, typeof(Enum[]));
                    flag      = enumArray.Length > 1;
                }
                else
                {
                    enumArray = new Enum[] { (Enum)value };
                    flag      = true;
                }
                CodeTypeReferenceExpression targetObject = new CodeTypeReferenceExpression(value.GetType());
                TypeConverter converter2 = new EnumConverter(value.GetType());
                foreach (Enum enum2 in enumArray)
                {
                    string         str   = (converter2 != null) ? converter2.ConvertToString(enum2) : null;
                    CodeExpression right = !string.IsNullOrEmpty(str) ? new CodeFieldReferenceExpression(targetObject, str) : null;
                    if (right != null)
                    {
                        if (left == null)
                        {
                            left = right;
                        }
                        else
                        {
                            left = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BitwiseOr, right);
                        }
                    }
                }
                if ((left != null) && flag)
                {
                    left = new CodeCastExpression(value.GetType(), left);
                }
            }
            return(left);
        }
Exemplo n.º 10
0
        private static string AnimationCurveToString(object value)
        {
            if (value == null)
            {
                return(string.Empty);
            }
            AnimationCurve curve     = (AnimationCurve)value;
            StringBuilder  builder   = new StringBuilder();
            EnumConverter  converter = new EnumConverter(typeof(WrapMode));

            builder.AppendFormat("{0}\n{1}", converter.ConvertToString(curve.postWrapMode), converter.ConvertToString(curve.preWrapMode));
            foreach (Keyframe keyframe in curve.keys)
            {
                object[] args = new object[] { InvStr(keyframe.inTangent), InvStr(keyframe.outTangent), InvStr(keyframe.time), InvStr(keyframe.value) };
                builder.AppendFormat("\n{0}, {1}, {2}, {3}", args);
            }
            return(builder.ToString());
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            Console.WriteLine("使用 EnumConverter ");
            {
                EnumConverter converter = new EnumConverter(typeof(ProgrammingLanguage));

                // 将字符串转换为枚举.
                string langStr = "CS, Cpp, XAML";
                Console.WriteLine("将字符串 \"{0}\" 转换为枚举...", langStr);
                ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr);
                Console.WriteLine("完成!");

                // 将枚举转换为字符串.
                Console.WriteLine("将枚举结果转换为字符串...");
                langStr = converter.ConvertToString(lang);
                Console.WriteLine("完成! \"{0}\"", langStr);
            }

            Console.WriteLine("\n使用 EnumDescriptionConverter ");
            {
                EnumDescriptionConverter converter = new EnumDescriptionConverter(
                    typeof(ProgrammingLanguage));

                // 将枚举转换为字符串.
                string langStr = "Visual C#, Visual C++, XAML";
                Console.WriteLine("将字符串 \"{0}\" 转换为枚举...", langStr);
                ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr);
                Console.WriteLine("完成!");

                // 将枚举转换为字符串.
                Console.WriteLine("将枚举结果转换为字符串...");
                langStr = converter.ConvertToString(lang);
                Console.WriteLine("完成! \"{0}\"", langStr);
            }


            Console.ReadLine();
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            Console.WriteLine("Using EnumConverter to");
            {
                EnumConverter converter = new EnumConverter(typeof(ProgrammingLanguage));

                // Convert string to enum.
                string langStr = "CS, Cpp, XAML";
                Console.WriteLine("Convert the string \"{0}\" to enum...", langStr);
                ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr);
                Console.WriteLine("Done!");

                // Convert enum to string.
                Console.WriteLine("Convert the resulting enum to string...");
                langStr = converter.ConvertToString(lang);
                Console.WriteLine("Done! \"{0}\"", langStr);
            }

            Console.WriteLine("\nUsing EnumDescriptionConverter to");
            {
                EnumDescriptionConverter converter = new EnumDescriptionConverter(
                    typeof(ProgrammingLanguage));

                // Convert string to enum.
                string langStr = "Visual C#, Visual C++, XAML";
                Console.WriteLine("Convert the string \"{0}\" to enum...", langStr);
                ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr);
                Console.WriteLine("Done!");

                // Convert enum to string.
                Console.WriteLine("Convert the resulting enum to string...");
                langStr = converter.ConvertToString(lang);
                Console.WriteLine("Done! \"{0}\"", langStr);
            }

            Console.ReadLine();
        }
Exemplo n.º 13
0
        /// <summary>
        ///     Serializes the given object into a CodeDom object.
        /// </summary>
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            CodeExpression expression = null;

            using (TraceScope("EnumCodeDomSerializer::" + nameof(Serialize)))
            {
                Trace("Type: {0}", (value == null ? "(null)" : value.GetType().Name));
                if (value is Enum)
                {
                    bool          needCast = false;
                    Enum[]        values;
                    TypeConverter converter = TypeDescriptor.GetConverter(value);
                    if (converter != null && converter.CanConvertTo(typeof(Enum[])))
                    {
                        values   = (Enum[])converter.ConvertTo(value, typeof(Enum[]));
                        needCast = (values.Length > 1);
                    }
                    else
                    {
                        values   = new Enum[] { (Enum)value };
                        needCast = true;
                    }

                    // EnumConverter (and anything that is overridden to support enums)
                    // should be providing us a conversion to Enum[] for flag styles.
                    // If it doesn't, we will emit a warning and just do a cast from the enum value.

                    CodeTypeReferenceExpression enumType = new CodeTypeReferenceExpression(value.GetType());

                    // If names is of length 1, then this is a simple field reference. Otherwise,
                    // it is an or-d combination of expressions.
                    //
                    TraceIf(values.Length == 1, "Single field entity.");
                    TraceIf(values.Length > 1, "Multi field entity.");

                    // We now need to serialize the enum terms as fields. We new up an EnumConverter to do
                    // that. We cannot use the type's own converter since it might have a different string
                    // representation for its values. Hardcoding is okay in this case, since all we want is
                    // the enum's field name. Simply doing ToString() will not give us any validation.
                    TypeConverter enumConverter = new EnumConverter(value.GetType());
                    foreach (Enum term in values)
                    {
                        string         termString    = (enumConverter != null) ? enumConverter.ConvertToString(term) : null;
                        CodeExpression newExpression = !String.IsNullOrEmpty(termString) ? new CodeFieldReferenceExpression(enumType, termString) : null;

                        if (newExpression != null)
                        {
                            if (expression == null)
                            {
                                expression = newExpression;
                            }
                            else
                            {
                                expression = new CodeBinaryOperatorExpression(expression, CodeBinaryOperatorType.BitwiseOr, newExpression);
                            }
                        }
                    }

                    // If we had to combine multiple names, wrap the result in an appropriate cast.
                    //
                    if (expression != null && needCast)
                    {
                        expression = new CodeCastExpression(value.GetType(), expression);
                    }
                }
                else
                {
                    Debug.Fail("Enum serializer called for non-enum object.");
                    TraceError("Enum serializer called for non-enum object {0}", (value == null ? "(null)" : value.GetType().Name));
                }
            }

            return(expression);
        }
Exemplo n.º 14
0
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     return(destinationType == typeof(string) && value is PopupSize popupSize?
            enumConverter.ConvertToString(popupSize.Width) + "; " + popupSize.Height:
            base.ConvertTo(context, culture, value, destinationType));
 }
Exemplo n.º 15
0
 public ContentTypeHeader(ContentType contentType)
 {
     HeaderPrefix     = "Content-Type:";
     this.contentType = EnumConverter.ConvertToString(contentType);
 }