private static object GetPropertyValue(ActivityDataLog dataLog, PropertyInfo property, DataLogPropertyAttribute dataLogAttribute) { if (dataLogAttribute.MaxLength == 0 || dataLogAttribute.MaxLength < -1) { throw new DataLoggerMockException("Data log attribute max length must be a positive number or -1 (for unlimited)."); } // If it's a string, we might need some special handling if (property.PropertyType == typeof(string)) { string value = property.GetValue(dataLog) as string ?? string.Empty; // If there is a maximum length specified and the value exceeds that length... if (dataLogAttribute.MaxLength > 0 && value.Length > dataLogAttribute.MaxLength) { // If trunctation is allowed, truncate the value and return if (dataLogAttribute.TruncationAllowed) { return(value.Substring(0, dataLogAttribute.MaxLength)); } else { // The value exceeds the maximum length, but truncation is not allowed. throw new DataLoggerMockException($"Value of property '{property.Name}' exceeds the max length, and truncation is not allowed."); } } } return(property.GetValue(dataLog)); }
public DataLogPropertyInfo(PropertyInfo propertyInfo, DataLogPropertyAttribute dataLogPropertyAttribute) { PropertyInfo = propertyInfo; DataLogPropertyAttribute = dataLogPropertyAttribute; }