Пример #1
0
        /// <summary>
        /// Returns a formatted value for the attribute <paramref name="value"/>. If the value is a catalog entry, the catalog value will
        /// be returned. This method uses the <paramref name="provider"/> for formatting the value. If the <paramref name="provider"/>
        /// is not specified, the <see cref="CultureInfo.CurrentUICulture"/> is used to format the resulting value.
        /// </summary>
        public string GetFormattedValue(ushort key, string value, CatalogCollection catalogs, IFormatProvider provider = null)
        {
            if (value == null)
            {
                return(null);
            }

            var def = GetDefinition(key);

            if (def is CatalogAttributeDefinition)
            {
                if (catalogs != null)
                {
                    var entry = catalogs[((CatalogAttributeDefinition)def).Catalog, value];
                    if (entry != null)
                    {
                        return(entry.ToString(CultureInfo.InvariantCulture));
                    }
                }
            }
            else if (value.Length > 0)
            {
                try
                {
                    var attDef = (AttributeDefinition)def;
                    switch (attDef.Type)
                    {
                    case AttributeType.Integer:
                        return(int.Parse(value, CultureInfo.InvariantCulture).ToString(provider ?? CultureInfo.CurrentUICulture));

                    case AttributeType.Float:
                        return(double.Parse(value, CultureInfo.InvariantCulture).ToString(provider ?? CultureInfo.CurrentUICulture));

                    case AttributeType.DateTime:
                        return(XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.RoundtripKind).ToString(provider ?? CultureInfo.CurrentUICulture));
                    }
                }
                catch
                {
                    // ignored
                }
            }
            return(value);
        }
Пример #2
0
        /// <summary>
        /// Returns the value represented by <paramref name="attributeValue"/>. <paramref name="attributeValue"/> musst be the language neutral database entry of an attribute.
        /// I.e. if an attribute represents a catatalogue entry, the catalog entry is returned. If an attribute represents a normal value
        /// of type string, int, double or DateTime the value is returned with that type.
        /// </summary>
        public object ParseValue(ushort key, string attributeValue, CatalogCollection catalogs)
        {
            object result = attributeValue;

            if (attributeValue != null)
            {
                var def        = GetDefinition(key);
                var definition = def as CatalogAttributeDefinition;
                if (definition != null)
                {
                    if (catalogs != null)
                    {
                        result = catalogs[definition.Catalog, attributeValue];
                    }
                }
                else if (attributeValue.Length > 0)
                {
                    try
                    {
                        var attDef = ( AttributeDefinition )def;
                        switch (attDef.Type)
                        {
                        case AttributeType.Integer: result = int.Parse(attributeValue, CultureInfo.InvariantCulture); break;

                        case AttributeType.Float: result = double.Parse(attributeValue, CultureInfo.InvariantCulture); break;

                        case AttributeType.DateTime: result = XmlConvert.ToDateTime(attributeValue, XmlDateTimeSerializationMode.RoundtripKind); break;
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
            return(result ?? attributeValue);
        }