title: Is there a way to detect whether #pragma unmanaged is in effect in C++/CLI?
body: <p>I have a project that includes some performance sensitive native C++ headers making heavy use of templates.  For this project we also wrap the headers and add some glue code to expose the functionality to c# and other .NET languages.  We'll call this header "layout.h", and we'll assume it's a third party header that I can't change.</p>
  
  <p>In a mixed mode C++/CLI assembly it is relatively easy to make a mistake and #include  from a place in the code where #pragma unmanaged (or #pramga managed(push,off) ) .  When that happens the templates generate IL, and I get extra managed/unmanaged transitions when running the code and performance goes down the drain.</p>
  
  <p>My question is whether there is a way I can do a compile-time check just before the #include so that compilation fails if I am accidently #including from the wrong context.</p>
  
  <pre><code>// File1.cpp, compiled in a mixed mode C++/CLI assembly with /clr
      ASSERT_UNMANAGED()
      #include &lt;layout.h&gt;
  </code></pre>
  
  <p>My naive 1st attempt checked #ifdef _MANAGED, but that is always defined whether I'm in a #pragma unmanaged block of code or not.  </p>