@@ -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 ) ;
0 commit comments