Skip to content

Commit b31c054

Browse files
authored
Allow BeginScope to perform reflection of KeyValuePair (#813)
1 parent ad5bbca commit b31c054

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private static bool TryLookupExtractor<TState>(ExtractorDictionary stateExtracto
240240
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET471_OR_GREATER
241241
if (propertyValue is System.Runtime.CompilerServices.ITuple tuple && tuple.Length == 2 && tuple[0] is string)
242242
{
243-
keyValueExtractor = (obj) => new KeyValuePair<string, object?>(
243+
keyValueExtractor = static (obj) => new KeyValuePair<string, object?>(
244244
((System.Runtime.CompilerServices.ITuple)obj)[0]?.ToString() ?? string.Empty,
245245
((System.Runtime.CompilerServices.ITuple)obj)[1]);
246246
return true;
@@ -279,8 +279,8 @@ private static bool TryBuildExtractor(Type propertyType, out Func<object, KeyVal
279279
if (itemType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
280280
{
281281
#if NETSTANDARD || NETFRAMEWORK
282-
var keyPropertyInfo = typeof(KeyValuePair<,>).MakeGenericType(itemType.GenericTypeArguments).GetTypeInfo().GetDeclaredProperty("Key");
283-
var valuePropertyInfo = typeof(KeyValuePair<,>).MakeGenericType(itemType.GenericTypeArguments).GetTypeInfo().GetDeclaredProperty("Value");
282+
var keyPropertyInfo = typeof(KeyValuePair<,>).MakeGenericType(itemType.GenericTypeArguments).GetTypeInfo().GetDeclaredProperty(nameof(KeyValuePair<string, object>.Key));
283+
var valuePropertyInfo = typeof(KeyValuePair<,>).MakeGenericType(itemType.GenericTypeArguments).GetTypeInfo().GetDeclaredProperty(nameof(KeyValuePair<string, object>.Value));
284284
if (valuePropertyInfo is null || keyPropertyInfo is null)
285285
{
286286
return false;
@@ -294,22 +294,7 @@ private static bool TryBuildExtractor(Type propertyType, out Func<object, KeyVal
294294
#else
295295
if (itemType.GenericTypeArguments[0] == typeof(string))
296296
{
297-
if (itemType.GenericTypeArguments[1] == typeof(object))
298-
return BuildKeyValueExtractor<string, object>(propertyType, out keyValueExtractor);
299-
if (itemType.GenericTypeArguments[1] == typeof(string))
300-
return BuildKeyValueExtractor<string, string>(propertyType, out keyValueExtractor);
301-
if (itemType.GenericTypeArguments[1] == typeof(int))
302-
return BuildKeyValueExtractor<string, int>(propertyType, out keyValueExtractor);
303-
if (itemType.GenericTypeArguments[1] == typeof(long))
304-
return BuildKeyValueExtractor<string, long>(propertyType, out keyValueExtractor);
305-
if (itemType.GenericTypeArguments[1] == typeof(decimal))
306-
return BuildKeyValueExtractor<string, decimal>(propertyType, out keyValueExtractor);
307-
if (itemType.GenericTypeArguments[1] == typeof(double))
308-
return BuildKeyValueExtractor<string, double>(propertyType, out keyValueExtractor);
309-
if (itemType.GenericTypeArguments[1] == typeof(bool))
310-
return BuildKeyValueExtractor<string, bool>(propertyType, out keyValueExtractor);
311-
if (itemType.GenericTypeArguments[1] == typeof(Guid))
312-
return BuildKeyValueExtractor<string, Guid>(propertyType, out keyValueExtractor);
297+
return BuildKeyValueExtractor(propertyType, out keyValueExtractor);
313298
}
314299
#endif
315300
}
@@ -335,10 +320,74 @@ private static bool TryBuildExtractor(Type propertyType, out Func<object, KeyVal
335320
}
336321

337322
#if !NETSTANDARD && !NETFRAMEWORK
338-
private static bool BuildKeyValueExtractor<TKey, TValue>(Type propertyType, out Func<object, KeyValuePair<string, object?>>? keyValueExtractor)
323+
private static KeyValuePair<string, object?> TypedKeyValueExtractor<TKey, TValue>(object value)
339324
{
340-
var keyPropertyInfo = typeof(KeyValuePair<TKey, TValue>).GetTypeInfo().GetDeclaredProperty("Key");
341-
var valuePropertyInfo = typeof(KeyValuePair<TKey, TValue>).GetTypeInfo().GetTypeInfo().GetDeclaredProperty("Value");
325+
var keyValuePair = (KeyValuePair<TKey, TValue>)value;
326+
return new KeyValuePair<string, object?>(keyValuePair.Key?.ToString() ?? string.Empty, keyValuePair.Value);
327+
}
328+
329+
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming - Allow reflection of BeginScope args", "IL2070")]
330+
private static bool BuildKeyValueExtractor(Type propertyType, out Func<object, KeyValuePair<string, object?>>? keyValueExtractor)
331+
{
332+
var itemType = propertyType.GetTypeInfo();
333+
if (itemType.GenericTypeArguments[1] == typeof(object))
334+
{
335+
keyValueExtractor = TypedKeyValueExtractor<string, object>;
336+
return true;
337+
}
338+
if (itemType.GenericTypeArguments[1] == typeof(string))
339+
{
340+
keyValueExtractor = TypedKeyValueExtractor<string, string>;
341+
return true;
342+
}
343+
if (itemType.GenericTypeArguments[1] == typeof(int))
344+
{
345+
keyValueExtractor = TypedKeyValueExtractor<string, int>;
346+
return true;
347+
}
348+
if (itemType.GenericTypeArguments[1] == typeof(long))
349+
{
350+
keyValueExtractor = TypedKeyValueExtractor<string, long>;
351+
return true;
352+
}
353+
if (itemType.GenericTypeArguments[1] == typeof(decimal))
354+
{
355+
keyValueExtractor = TypedKeyValueExtractor<string, decimal>;
356+
return true;
357+
}
358+
if (itemType.GenericTypeArguments[1] == typeof(double))
359+
{
360+
keyValueExtractor = TypedKeyValueExtractor<string, double>;
361+
return true;
362+
}
363+
if (itemType.GenericTypeArguments[1] == typeof(bool))
364+
{
365+
keyValueExtractor = TypedKeyValueExtractor<string, bool>;
366+
return true;
367+
}
368+
if (itemType.GenericTypeArguments[1] == typeof(Guid))
369+
{
370+
keyValueExtractor = TypedKeyValueExtractor<string, Guid>;
371+
return true;
372+
}
373+
if (itemType.GenericTypeArguments[1] == typeof(DateTime))
374+
{
375+
keyValueExtractor = TypedKeyValueExtractor<string, DateTime>;
376+
return true;
377+
}
378+
if (itemType.GenericTypeArguments[1] == typeof(DateTimeOffset))
379+
{
380+
keyValueExtractor = TypedKeyValueExtractor<string, DateTimeOffset>;
381+
return true;
382+
}
383+
if (itemType.GenericTypeArguments[1] == typeof(TimeSpan))
384+
{
385+
keyValueExtractor = TypedKeyValueExtractor<string, TimeSpan>;
386+
return true;
387+
}
388+
389+
var keyPropertyInfo = propertyType.GetTypeInfo().GetDeclaredProperty(nameof(KeyValuePair<string,object>.Key));
390+
var valuePropertyInfo = propertyType.GetTypeInfo().GetDeclaredProperty(nameof(KeyValuePair<string, object>.Value));
342391
if (valuePropertyInfo is null || keyPropertyInfo is null)
343392
{
344393
keyValueExtractor = default;
@@ -369,4 +418,4 @@ private static bool BuildKeyValueExtractor(ParameterExpression keyValuePairObjPa
369418
return true;
370419
}
371420
}
372-
}
421+
}

0 commit comments

Comments
 (0)