diff options
author | jbhensley <jbhensley@users.noreply.github.com> | 2018-10-12 17:25:19 -0700 |
---|---|---|
committer | Noah Falk <noahfalk@users.noreply.github.com> | 2018-10-12 17:25:19 -0700 |
commit | d6c35b6274a49bf83eff4e1089b8013c3741d936 (patch) | |
tree | f4430df4e693848fb044585f0c85a8ca78faa987 | |
parent | e0637d0e83582543e7367a417465438c14120b8d (diff) | |
download | coreclr-d6c35b6274a49bf83eff4e1089b8013c3741d936.tar.gz coreclr-d6c35b6274a49bf83eff4e1089b8013c3741d936.tar.bz2 coreclr-d6c35b6274a49bf83eff4e1089b8013c3741d936.zip |
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)
2 files changed, 13 insertions, 4 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs index e0a9374790..dc714d860e 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs @@ -269,7 +269,6 @@ namespace System.Diagnostics.Tracing internal sealed class NullableTypeInfo : TraceLoggingTypeInfo { private readonly TraceLoggingTypeInfo valueInfo; - private readonly Func<PropertyValue, PropertyValue> hasValueGetter; private readonly Func<PropertyValue, PropertyValue> valueGetter; public NullableTypeInfo(Type type, List<Type> recursionCheck) @@ -278,7 +277,6 @@ namespace System.Diagnostics.Tracing 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 @@ namespace System.Diagnostics.Tracing 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/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs index f6d0a59aa6..c6f89c8784 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs @@ -85,6 +85,15 @@ namespace System.Diagnostics.Tracing } /// <summary> + /// Adds a Boolean value to the event payload. + /// </summary> + /// <param name="value">Value to be added.</param> + public void AddScalar(bool value) + { + DataCollector.ThreadInstance.AddScalar(&value, sizeof(bool)); + } + + /// <summary> /// Adds a null-terminated String value to the event payload. /// </summary> /// <param name="value"> |