From b1f1b2cbc77113caf0d1d50325c451ae6189d1eb Mon Sep 17 00:00:00 2001 From: jbhensley Date: Fri, 12 Oct 2018 17:25:19 -0700 Subject: [PATCH] Fix "Non-static method requires a target" caused by trying to access the HasValue property of a nullable type through reflection when the value is null. (#20350) Signed-off-by: dotnet-bot --- .../Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs | 8 ++++---- .../Tracing/TraceLogging/TraceLoggingDataCollector.cs | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs b/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs index e0a937479037..dc714d860ec8 100644 --- a/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs +++ b/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs @@ -269,7 +269,6 @@ public override void WriteData(TraceLoggingDataCollector collector, PropertyValu internal sealed class NullableTypeInfo : TraceLoggingTypeInfo { private readonly TraceLoggingTypeInfo valueInfo; - private readonly Func hasValueGetter; private readonly Func valueGetter; public NullableTypeInfo(Type type, List recursionCheck) @@ -278,7 +277,6 @@ public NullableTypeInfo(Type type, List recursionCheck) var typeArgs = type.GenericTypeArguments; Debug.Assert(typeArgs.Length == 1); this.valueInfo = TraceLoggingTypeInfo.GetInstance(typeArgs[0], recursionCheck); - this.hasValueGetter = PropertyValue.GetPropertyGetter(type.GetTypeInfo().GetDeclaredProperty("HasValue")); this.valueGetter = PropertyValue.GetPropertyGetter(type.GetTypeInfo().GetDeclaredProperty("Value")); } @@ -294,9 +292,11 @@ public override void WriteMetadata( public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value) { - var hasValue = hasValueGetter(value); + // It's not currently possible to get the HasValue property of a nullable type through reflection when the + // value is null. Instead, we simply check that the nullable is not null. + var hasValue = value.ReferenceValue != null; collector.AddScalar(hasValue); - var val = hasValue.ScalarValue.AsBoolean ? valueGetter(value) : valueInfo.PropertyValueFactory(Activator.CreateInstance(valueInfo.DataType)); + var val = hasValue ? valueGetter(value) : valueInfo.PropertyValueFactory(Activator.CreateInstance(valueInfo.DataType)); this.valueInfo.WriteData(collector, val); } } diff --git a/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs b/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs index f6d0a59aa623..c6f89c8784e6 100644 --- a/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs +++ b/src/Common/src/CoreLib/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs @@ -84,6 +84,15 @@ public void AddScalar(double value) DataCollector.ThreadInstance.AddScalar(&value, sizeof(double)); } + /// + /// Adds a Boolean value to the event payload. + /// + /// Value to be added. + public void AddScalar(bool value) + { + DataCollector.ThreadInstance.AddScalar(&value, sizeof(bool)); + } + /// /// Adds a null-terminated String value to the event payload. ///