Search code examples
c#reflection.net-assemblymscorlib

Assembly reflection problem in Release mode


I'm facing some weird behavior trying to get the parent assembly in some logging class (only when it's compiled in Release mode).

In Debug mode this works like a charm:

StackFrame[] frames = new StackTrace().GetFrames();
var assemblies = (from f in frames
                  select f.GetMethod().ReflectedType.Assembly)
                  Distinct()
                  .Last();

Example: assembly A => assembly B => method

when I run the above script in debug mode I get assembly A (as expected) but when it runs in release mode I get: mscorlib instead. but the most weird thing is that if a check for the whole assemblies stack there is not a single reference of assembly A. how is this possible? what can be happening?

PS: assembly A is a webapp project.


Solution

  • I was able to solve this issue. I think this problem is related with the compiler behaviour in release mode. I think it has something to be with the method inlining (even when I can't not be sure).

    I found this conditions in order to prohibit the JIT from being able to inline a method (from here):

    • Methods that are greater than 32 bytes of IL will not be inlined.
    • Virtual functions are not inlined.
    • Methods that have complex flow control will not be in-lined. Complex flow control is any flow control other than if/then/else; in this case, switch or while.
    • Methods that contain exception-handling blocks are not inlined, though methods that throw exceptions are still candidates for inlining.
    • If any of the method's formal arguments are structs, the method will not be inlined.

    I tried with all the above recommendations, but the only one that worked for me was to add a parameter (optional) to my method in order to get it greater than 32 bytes.

    Hope this helps to anyone!