Ejemplo n.º 1
0
        /// <summary>
        /// Truncates the string values.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="encoding">The encoding.</param>
        /// <param name="stringBytesLimit">The string bytes limit.</param>
        protected void TruncateStringValues(IDictionary <string, object> dictionary, Encoding encoding, int stringBytesLimit)
        {
            Assumption.AssertNotNull(dictionary, nameof(dictionary));

            var keys = Enumerable.ToArray(dictionary.Keys);

            foreach (var key in keys)
            {
                string originalString = dictionary[key] as string;
                if (originalString != null)
                {
                    string truncatedString = StringUtility.Truncate(originalString, encoding, stringBytesLimit);
                    if (!object.ReferenceEquals(originalString, truncatedString))
                    {
                        dictionary[key] = truncatedString;
                    }
                    continue;
                }

                var objectDictionary = dictionary[key] as IDictionary <string, object>;
                if (objectDictionary != null)
                {
                    TruncateStringValues(objectDictionary, encoding, stringBytesLimit);
                    continue;
                }

                var stringDictionary = dictionary[key] as IDictionary <string, string>;
                if (stringDictionary != null)
                {
                    TruncateStringValues(stringDictionary, encoding, stringBytesLimit);
                    continue;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Truncates the string values.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="encoding">The encoding.</param>
        /// <param name="stringBytesLimit">The string bytes limit.</param>
        protected void TruncateStringValues(IDictionary <string, string> dictionary, Encoding encoding, int stringBytesLimit)
        {
            Assumption.AssertNotNull(dictionary, nameof(dictionary));

            var keys = Enumerable.ToArray(dictionary.Keys);

            foreach (var key in keys)
            {
                string original = dictionary[key];
                if (original != null)
                {
                    string truncated = StringUtility.Truncate(original, encoding, stringBytesLimit);
                    if (!object.ReferenceEquals(original, truncated))
                    {
                        dictionary[key] = truncated;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Truncates the strings.
        /// </summary>
        /// <param name="encoding">The encoding.</param>
        /// <param name="stringBytesLimit">The string bytes limit.</param>
        /// <returns></returns>
        public virtual DtoBase TruncateStrings(Encoding encoding, int stringBytesLimit)
        {
            foreach (var property in this.StringProperties)
            {
                string originalString  = property.GetValue(this) as string;
                string truncatedString = StringUtility.Truncate(originalString, encoding, stringBytesLimit);

                if (!object.ReferenceEquals(originalString, truncatedString))
                {
                    property.SetValue(this, truncatedString);
                }
            }

            foreach (var property in this.DictionaryProperties)
            {
                var objectDictionary = property.GetValue(this) as IDictionary <string, object>;
                if (objectDictionary != null)
                {
                    TruncateStringValues(objectDictionary, encoding, stringBytesLimit);
                    property.SetValue(this, objectDictionary);
                    continue;
                }

                var stringDictionary = property.GetValue(this) as IDictionary <string, string>;
                if (stringDictionary != null)
                {
                    TruncateStringValues(stringDictionary, encoding, stringBytesLimit);
                    property.SetValue(this, stringDictionary);
                    continue;
                }
            }

            foreach (var property in this.DtoProperties)
            {
                DtoBase dto = property.GetValue(this) as DtoBase;
                if (dto != null)
                {
                    dto.TruncateStrings(encoding, stringBytesLimit);
                }
            }

            foreach (var property in this.EnumerableProperties)
            {
                var enumerableItemTypes = property.PropertyType.GetInterfaces()
                                          .Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                                          .Select(t => t.GetGenericArguments()[0])
                                          .ToArray()
                ;

                if (enumerableItemTypes == null || enumerableItemTypes.Length != 1)
                {
                    continue;
                }
                else if (DtoBase.dtoPropertiesByType.Keys.Contains(enumerableItemTypes[0]))
                {
                    var dtos = property.GetValue(this) as IEnumerable <DtoBase>;
                    if (dtos == null)
                    {
                        continue;
                    }
                    foreach (var dto in dtos)
                    {
                        dto.TruncateStrings(encoding, stringBytesLimit);
                    }
                }
                else if (enumerableItemTypes[0] == typeof(string))
                {
                    var stringArray = property.GetValue(this) as string[];
                    if (stringArray == null)
                    {
                        continue;
                    }
                    List <string> truncatedStrings = new List <string>();
                    foreach (var str in stringArray)
                    {
                        truncatedStrings.Add(StringUtility.Truncate(str, encoding, stringBytesLimit));
                    }
                    property.SetValue(this, truncatedStrings.ToArray());
                }
            }

            return(this);
        }