Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20572 from jorive/dev/port-of-pr-20350
Browse files Browse the repository at this point in the history
Fix System.Reflection.TargetException: Non-static method requires a target
  • Loading branch information
wtgodbe authored Feb 13, 2019
2 parents 0b766ee + 6d9dabe commit 75d1d39
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ public override void WriteData(TraceLoggingDataCollector collector, PropertyValu
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)
Expand All @@ -278,7 +277,6 @@ public NullableTypeInfo(Type type, List<Type> 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"));
}

Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public void AddScalar(double value)
DataCollector.ThreadInstance.AddScalar(&value, sizeof(double));
}

/// <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>
Expand Down

0 comments on commit 75d1d39

Please sign in to comment.