How to make a method return an arbitrary struct using <T> magic in C#?
<p>Title sums it up.  I want to be able to call </p>

<pre><code>Foo f = getStruct&lt;Foo&gt;(..) 
</code></pre>

<p>and have the method create a new Foo object (it would be a struct), populate it, and return it?  </p>

<p>Also, what is the &lt; T > type of constructor called?  My google-searches are failing as I'm sure what I should be searching for.. </p>

<p>Additionally, I know that all the structs that can be created have a .raw field.  I want to be able to populate that .raw field.</p>

<p>This is essentially what I want to do.  </p>

<pre><code>    public T getStruct&lt;T&gt;(UInt32 sector) {
        &lt;T&gt; foo = new &lt;T&gt;;

        for (int i=0; i&lt;100; i++) foo.raw[i]=0;
        return foo;
    }
</code></pre>

<p>Where the structs have the form of </p>

<pre><code>  StructLayout(LayoutKind.Explicit)]
  public unsafe struct RunBlock_t {
    [System.Runtime.InteropServices.FieldOffset(0)]  public fixed byte raw[512];
    [System.Runtime.InteropServices.FieldOffset(0)]  public UInt16 run_id;
    [System.Runtime.InteropServices.FieldOffset(2)]  public UInt16 magic;
    [System.Runtime.InteropServices.FieldOffset(510)]  public UInt16 checksum;
  }
</code></pre>

<p>The function passes the .raw pointer and the sector to another function which loads that sector from the disk, then byte by byte copies the contents back into .raw</p>

<p>In this way, I can create an arbitrary struct and populate it from disk quickly and simply.  This is not very C# friendly, I know, but there are other external dependencies which require it.</p>

<p>Thnx!</p>
