Show In Stack Trace - CSharp Reflection

CSharp examples for Reflection:Type

Description

Show In Stack Trace

Demo Code

// Copyright (c) Ben A Adams. All rights reserved.
using System.Threading.Tasks;
using System.Threading;
using System.Text;
using System.Security;
using System.Runtime.ExceptionServices;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Linq;
using System.IO;/*from w w w . ja v a 2 s .  co  m*/
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        private static bool ShowInStackTrace(MethodBase method) {
            try {
                var type = method.DeclaringType;
                if (type == null) {
                    return true;
                }

                if (type == typeof(Task<>) && method.Name == "InnerInvoke") {
                    return false;
                }

                if (type == typeof(Task)) {
                    switch (method.Name) {
                        case "ExecuteWithThreadLocal":
                        case "Execute":
                        case "ExecutionContextCallback":
                        case "ExecuteEntry":
                        case "InnerInvoke":
                            return false;
                    }
                }

                if (type == typeof(ExecutionContext)) {
                    switch (method.Name) {
                        case "RunInternal":
                        case "Run":
                            return false;
                    }
                }

                // Don't show any methods marked with the StackTraceHiddenAttribute
                // https://github.com/dotnet/coreclr/pull/14652
                foreach (var attibute in method.GetCustomAttributesData()) {
                    // internal Attribute, match on name
                    if (attibute.AttributeType.Name == "StackTraceHiddenAttribute") {
                        return false;
                    }
                }

                foreach (var attibute in type.GetCustomAttributesData()) {
                    // internal Attribute, match on name
                    if (attibute.AttributeType.Name == "StackTraceHiddenAttribute") {
                        return false;
                    }
                }

                // Fallbacks for runtime pre-StackTraceHiddenAttribute
                if (type == typeof(ExceptionDispatchInfo) && method.Name == nameof(ExceptionDispatchInfo)) {
                    return false;
                }

                if (type == typeof(TaskAwaiter) ||
                    type == typeof(TaskAwaiter<>) ||
                    type == typeof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter) ||
                    type == typeof(ConfiguredTaskAwaitable<>.ConfiguredTaskAwaiter)) {
                    switch (method.Name) {
                        case "HandleNonSuccessAndDebuggerNotification":
                        case "ThrowForNonSuccess":
                        case "ValidateEnd":
                        case "GetResult":
                            return false;
                    }
                } else if (type.FullName == "System.ThrowHelper") {
                    return false;
                }
            } catch {
                // GetCustomAttributesData can throw
                return true;
            }

            return true;
        }
}

Related Tutorials