Skip to content

Commit 1b50176

Browse files
committed
PropertyTypeConverter - Apply IFormattable when Format specified
1 parent a43692e commit 1b50176

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/NLog/Config/PropertyTypeConverter.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ internal static bool IsComplexType(Type type)
115115

116116
private static bool TryConvertFromString(string propertyString, Type propertyType, string? format, IFormatProvider? formatProvider, out object? propertyValue)
117117
{
118-
propertyValue = propertyString = propertyString.Trim();
118+
propertyValue = propertyString;
119119

120120
if (StringConverterLookup.TryGetValue(propertyType, out var converter))
121121
{
122-
propertyValue = converter.Invoke(propertyString, format, formatProvider);
122+
propertyValue = converter.Invoke(propertyString?.Trim() ?? string.Empty, format, formatProvider);
123123
return true;
124124
}
125125

126126
if (propertyType.IsEnum)
127127
{
128-
return NLog.Common.ConversionHelpers.TryParseEnum(propertyString, propertyType, out propertyValue);
128+
return NLog.Common.ConversionHelpers.TryParseEnum(propertyString?.Trim() ?? string.Empty, propertyType, out propertyValue);
129129
}
130130

131131
if (PropertyHelper.TryTypeConverterConversion(propertyType, propertyString, out var convertedValue))
@@ -139,27 +139,35 @@ private static bool TryConvertFromString(string propertyString, Type propertyTyp
139139

140140
private static object? ChangeObjectType(object propertyValue, Type propertyType, string? format, IFormatProvider? formatProvider)
141141
{
142-
if (propertyValue is string propertyString && TryConvertFromString(propertyString, propertyType, format, formatProvider, out var fromStringValue))
142+
if (propertyValue is string propertyString)
143143
{
144-
return fromStringValue;
144+
if (TryConvertFromString(propertyString, propertyType, format, formatProvider, out var fromStringValue))
145+
return fromStringValue;
145146
}
146-
147-
if (propertyValue is IConvertible convertibleValue)
147+
else if (!string.IsNullOrEmpty(format) && propertyValue is IFormattable formattableValue)
148148
{
149-
var typeCode = convertibleValue.GetTypeCode();
150-
if (typeCode == TypeCode.DBNull)
151-
return convertibleValue;
152-
if (typeCode == TypeCode.Empty)
153-
return null;
154-
}
155-
else if (TryConvertToType(propertyValue, propertyType, out var convertedValue))
156-
{
157-
return convertedValue;
158-
}
149+
var stringValue = formattableValue.ToString(format, formatProvider);
150+
if (TryConvertFromString(stringValue, propertyType, format, formatProvider, out var fromStringValue))
151+
return fromStringValue;
159152

160-
if (!string.IsNullOrEmpty(format) && propertyValue is IFormattable formattableValue)
153+
propertyValue = stringValue;
154+
}
155+
else
161156
{
162-
propertyValue = formattableValue.ToString(format, formatProvider);
157+
if (propertyValue is IConvertible convertibleValue)
158+
{
159+
var typeCode = convertibleValue.GetTypeCode();
160+
if (typeCode == TypeCode.DBNull)
161+
return convertibleValue;
162+
if (typeCode == TypeCode.Empty)
163+
return null;
164+
if (typeCode == TypeCode.DateTime && propertyType == typeof(DateTimeOffset))
165+
return new DateTimeOffset(convertibleValue.ToDateTime(formatProvider));
166+
}
167+
else if (TryConvertToType(propertyValue, propertyType, out var convertedValue))
168+
{
169+
return convertedValue;
170+
}
163171
}
164172

165173
return System.Convert.ChangeType(propertyValue, propertyType, formatProvider);

tests/NLog.UnitTests/Config/PropertyTypeConverterTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,18 @@ public void Convert_FormattableToStringTest()
223223
Assert.Equal("0123", resultTyped);
224224
}
225225

226+
[Fact]
227+
public void Convert_DateTimeToDateTimeOffset()
228+
{
229+
// Act
230+
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
231+
var result = _sut.Convert(dateTime, typeof(DateTimeOffset), "o", null);
232+
233+
// Assert
234+
var resultTyped = Assert.IsType<DateTimeOffset>(result);
235+
Assert.Equal(dateTime, resultTyped.DateTime);
236+
}
237+
226238
[Fact]
227239
public void Convert_NullableFormattableToStringTest()
228240
{

0 commit comments

Comments
 (0)