title: C++/CLI Generic Static Class
body: <p>I've been trying to convert some C# code to C++/CLI code (for some performance reasons, as well as avoiding a bunch of interop issues), however I've hit a wall. Admittedly, my C++/CLI knowledge is lacking (at the absolute best).</p>
  <p>Trying to convert a generic static class, that we used for size caching, is proving to be a major headache.</p>
  <p>The C# class is as follows;</p>
  <pre><code>public static class SizeCache&lt;T&gt;
  {
      public static int Size = Marshal.SizeOf(typeof(T));
  }
  </code></pre>
  <p>And the C++/CLI wrapper (as far as I've gotten) is the following;</p>
  <pre><code>    generic &lt;typename T&gt;
  public ref class SizeCache abstract sealed
  {
  private:
      static SizeCache()
      {
          Size = Marshal::SizeOf(T::typeid);
      }
  
  public:
      static int Size;
  };
  </code></pre>
  <p>Everything seems to compile fine, however, when I access it via;</p>
  <pre><code>int size = SizeCache&lt;T&gt;::Size;
  </code></pre>
  <p>I get compiler errors.</p>
  <blockquote>
  <p>error C2039: 'Size' : is not a member of '`global namespace''</p>
  <p>error C2065: 'Size' : undeclared identifier</p>
  </blockquote>
  <p>Unfortunately, I can't see what I'm doing wrong in this case.</p>
  <p>The reason for the class in the first place, is to avoid some issues with the Marshaler (specifically when dealing with generic structure types), and avoid the performance hit incurred by constantly calling SizeOf on the same basic types.</p>
  <p>What am I doing wrong here?</p>