Fix "Non-static method requires a target" caused by trying to access the HasValue...
authorjbhensley <jbhensley@users.noreply.github.com>
Sat, 13 Oct 2018 00:25:19 +0000 (17:25 -0700)
committerNoah Falk <noahfalk@users.noreply.github.com>
Sat, 13 Oct 2018 00:25:19 +0000 (17:25 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/d6c35b6274a49bf83eff4e1089b8013c3741d936

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/TraceLoggingDataCollector.cs

index e0a9374..dc714d8 100644 (file)
@@ -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);
         }
     }
index f6d0a59..c6f89c8 100644 (file)
@@ -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">