Why I can not find a custom attribute in this method

I have a method with the following signature

[Specification] public void slide_serialization() { 

From a point in my code, I need to move the stack to find the closest method with SpecificationAttribute (performance is not a problem here). I find this method, but I cannot find any custom attributes.

No custom attributes

I don't think I've ever seen this. What could be the reason?

This is a modular test build with optimization disabled in Build.

+5
source share
1 answer

The code snippet is not much. But the stack trace makes it clear what happened. Note the type name <>c_DisplayClass5 in the trace. This is an automatically generated class created by the C # compiler when it rewrites your code to compile a lambda expression with closure. The theme of this is Q + A.

The slide_serialization () method has also been rewritten, now acquiring the name of the unspeakable <slide_serialization>_b40 . Using angle brackets is deliberate, this ensures that members in automatically generated code never encounter identifier names in your program.

And you found a limitation in the logic of rewriting code in the compiler. It does not pass [attributes] of source code to rewritten code. Regardless of whether Microsoft thought it was important enough to invest in this effort, or if they couldn’t do it right, it’s unclear for every possible code rewriting rule. I strongly suspect the latter, the restriction is rather painful. It is usually found with great chagrin by programmers who need the [SuppressMessage] attribute in order to undergo code analysis without warning.

There is no easy workaround for this; you need to deal with the restriction.

+5
source

Source: https://habr.com/ru/post/1205459/


All Articles