OpenRA.FieldSaver.FormatValue C# (CSharp) Method

FormatValue() public static method

public static FormatValue ( object v ) : string
v object
return string
        public static string FormatValue(object v)
        {
            if (v == null)
                return "";

            var t = v.GetType();

            // Color.ToString() does the wrong thing; force it to format as rgb[a] hex
            if (t == typeof(Color))
            {
                return HSLColor.ToHexString((Color)v);
            }

            // HSLColor.ToString() does the wrong thing; force it to format as rgb[a] hex
            if (t == typeof(HSLColor))
            {
                return ((HSLColor)v).ToHexString();
            }

            if (t == typeof(ImageFormat))
            {
                return ((ImageFormat)v).ToString();
            }

            if (t == typeof(Rectangle))
            {
                var r = (Rectangle)v;
                return "{0},{1},{2},{3}".F(r.X, r.Y, r.Width, r.Height);
            }

            if (t.IsArray && t.GetArrayRank() == 1)
            {
                return ((Array)v).Cast<object>().Select(FormatValue).JoinWith(", ");
            }

            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(HashSet<>))
            {
                return ((System.Collections.IEnumerable)v).Cast<object>().Select(FormatValue).JoinWith(", ");
            }

            // This is only for documentation generation
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dictionary<,>))
            {
                var result = "";
                var dict = (System.Collections.IDictionary)v;
                foreach (var kvp in dict)
                {
                    var key = ((System.Collections.DictionaryEntry)kvp).Key;
                    var value = ((System.Collections.DictionaryEntry)kvp).Value;

                    var formattedKey = FormatValue(key);
                    var formattedValue = FormatValue(value);

                    result += "{0}: {1}{2}".F(formattedKey, formattedValue, Environment.NewLine);
                }

                return result;
            }

            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Primitives.Cache<,>))
                return ""; // TODO

            if (t == typeof(DateTime))
                return ((DateTime)v).ToString("yyyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture);

            // Try the TypeConverter
            var conv = TypeDescriptor.GetConverter(t);
            if (conv.CanConvertTo(typeof(string)))
            {
                try
                {
                    return conv.ConvertToInvariantString(v);
                }
                catch
                {
                }
            }

            return v.ToString();
        }

Same methods

FieldSaver::FormatValue ( object o, FieldInfo f ) : string

Usage Example

Example #1
0
        public void Serialize(Map map, List <MiniYamlNode> nodes)
        {
            var value = field != null?field.GetValue(map) : property.GetValue(map, null);

            if (type == Type.NodeList)
            {
                var listValue = (List <MiniYamlNode>)value;
                if (required || listValue.Any())
                {
                    nodes.Add(new MiniYamlNode(key, null, listValue));
                }
            }
            else if (type == Type.MiniYaml)
            {
                var yamlValue = (MiniYaml)value;
                if (required || (yamlValue != null && (yamlValue.Value != null || yamlValue.Nodes.Any())))
                {
                    nodes.Add(new MiniYamlNode(key, yamlValue));
                }
            }
            else
            {
                var formattedValue = FieldSaver.FormatValue(value);
                if (required || formattedValue != ignoreIfValue)
                {
                    nodes.Add(new MiniYamlNode(key, formattedValue));
                }
            }
        }
All Usage Examples Of OpenRA.FieldSaver::FormatValue