How to check if class attribute defined in StackFrame?

asked 2022-06-29T10:23:52.757+00:00
Y_O_R_O 21 Reputation points

I need to skip System.Diagnostics.StackFrame routes of my class in my exeption middleware.

Currently I can check in exception StackFrame if my methods have [StackTraceHidden] attribute.

For example this is how my class looks now (attribute needs to mark every method):

   public class ValidString  
   {  
       private string _value;  
     
       [StackTraceHidden]  
       public ValidString(string value)  
       {  
           Validate(value);  
           _value = value;  
       }  
     
       [StackTraceHidden]  
       private void Validate(string value)  
       {  
           if (string.IsNullOrWhiteSpace(value))  
               throw new ArgumentException(nameof(value));    
       }  
     
       [StackTraceHidden]  
       public static implicit operator ValidString(string value)  
       {  
           if (value == null)  
               return null;  
     
           return new ValidString(value);  
       }  
   }  

And how I skip marked methods in StackFrame:

 foreach(StackFrame stackFrame in stackTrace.GetFrames())  
        if (!stackFrame.GetMethod().IsDefined(typeof(StackTraceHiddenAttribute), true))  
            return stackFrame;  

It doesn't feel right to mark every method of classes I need to skip with these attributes. I want mark my class with [StackTraceHidden] attribute to skip all StackFrame's that are registered at this class.

This is how I want it to be (attribute only marks class):

   [StackTraceHidden]  
   public class ValidString  
   {  
       private string _value;  
     
       public ValidString(string value)  
       {  
           Validate(value);  
           _value = value;  
       }  
     
       private void Validate(string value)  
       {  
           if (string.IsNullOrWhiteSpace(value))  
               throw new ArgumentException(nameof(value));  
       }  
     
       public static implicit operator ValidString(string value)  
       {  
           if (value == null)  
               return null;  
     
           return new ValidString(value);  
       }  
   }  

And that's how I imagine to skip whole class from StackFrame:

 foreach(StackFrame stackFrame in stackTrace.GetFrames())  
        if (!stackFrame.GetClass().IsDefined(typeof(StackTraceHiddenAttribute), true))  
            return stackFrame;  

In short: How to do something like this?

bool markedWithSomeAttribute = stackTrace.GetFrame(i).GetClass().IsDefined(typeof(SomeAttribute), true);

Originally I asked this question here: https://stackoverflow.com/questions/72785811/how-to-check-if-class-attribute-defined-in-stackframe

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,527 questions
No comments
{count} votes

Accepted answer
  1. answered 2022-06-29T14:39:02.767+00:00
    Viorel 82,371 Reputation points

    Try something like this:

    bool marked = stackFrame.GetMethod( ).DeclaringType.CustomAttributes.Any( ca => ca.AttributeType == typeof( StackTraceHiddenAttribute ) );

    No comments

0 additional answers

Sort by: Most helpful