diff --git a/src/OpenTelemetry/Trace/ExceptionProcessor.cs b/src/OpenTelemetry/Trace/ExceptionProcessor.cs index ad6616f5f64..17023d46c80 100644 --- a/src/OpenTelemetry/Trace/ExceptionProcessor.cs +++ b/src/OpenTelemetry/Trace/ExceptionProcessor.cs @@ -17,9 +17,11 @@ #nullable enable using System.Diagnostics; +using System.Runtime.InteropServices; +#if !NET6_0_OR_GREATER && !NETFRAMEWORK using System.Linq.Expressions; using System.Reflection; -using System.Runtime.InteropServices; +#endif namespace OpenTelemetry.Trace; @@ -31,6 +33,11 @@ internal sealed class ExceptionProcessor : BaseProcessor public ExceptionProcessor() { +#if NET6_0_OR_GREATER || NETFRAMEWORK + this.fnGetExceptionPointers = Marshal.GetExceptionPointers; +#else + // When running on netstandard or similar the Marshal class is not a part of the netstandard API + // but it would still most likely be available in the underlying framework, so use reflection to retrieve it. try { var flags = BindingFlags.Static | BindingFlags.Public; @@ -43,6 +50,7 @@ public ExceptionProcessor() { throw new NotSupportedException($"'{typeof(Marshal).FullName}.GetExceptionPointers' is not supported", ex); } +#endif } /// diff --git a/src/Shared/ActivityInstrumentationHelper.cs b/src/Shared/ActivityInstrumentationHelper.cs index ea0b7af05db..0e841fe19d9 100644 --- a/src/Shared/ActivityInstrumentationHelper.cs +++ b/src/Shared/ActivityInstrumentationHelper.cs @@ -15,8 +15,6 @@ // using System.Diagnostics; -using System.Linq.Expressions; -using System.Reflection; #pragma warning restore IDE0005 namespace OpenTelemetry.Instrumentation; @@ -28,19 +26,13 @@ internal static class ActivityInstrumentationHelper private static Action CreateActivitySourceSetter() { - ParameterExpression instance = Expression.Parameter(typeof(Activity), "instance"); - ParameterExpression propertyValue = Expression.Parameter(typeof(ActivitySource), "propertyValue"); - PropertyInfo sourcePropertyInfo = typeof(Activity).GetProperty("Source"); - var body = Expression.Assign(Expression.Property(instance, sourcePropertyInfo), propertyValue); - return Expression.Lambda>(body, instance, propertyValue).Compile(); + return (Action)typeof(Activity).GetProperty("Source") + .SetMethod.CreateDelegate(typeof(Action)); } private static Action CreateActivityKindSetter() { - ParameterExpression instance = Expression.Parameter(typeof(Activity), "instance"); - ParameterExpression propertyValue = Expression.Parameter(typeof(ActivityKind), "propertyValue"); - PropertyInfo kindPropertyInfo = typeof(Activity).GetProperty("Kind"); - var body = Expression.Assign(Expression.Property(instance, kindPropertyInfo), propertyValue); - return Expression.Lambda>(body, instance, propertyValue).Compile(); + return (Action)typeof(Activity).GetProperty("Kind") + .SetMethod.CreateDelegate(typeof(Action)); } }