struct and destructor
<p>I created the type <code>IntPtr&lt;T&gt;</code> to act like a generic pointer that c/c++ have:</p>

<pre><code>public struct IntPtr&lt;T&gt; : IDisposable where T : struct
{
    private static Dictionary&lt;IntPtr, GCHandle&gt; handles = new Dictionary&lt;IntPtr, GCHandle&gt;();

    private IntPtr ptr;

    public IntPtr(ref T value)
    {
        GCHandle gc = GCHandle.Alloc(value, GCHandleType.Pinned);

        ptr = gc.AddrOfPinnedObject();
        handles.Add(ptr, gc);
    }

    public IntPtr(ref T[] value)
    {
        GCHandle gc = GCHandle.Alloc(value, GCHandleType.Pinned);

        ptr = gc.AddrOfPinnedObject();
        handles.Add(ptr, gc);
    }

    public IntPtr(IntPtr value)
    { ptr = value; }

    public IntPtr(IntPtr&lt;T&gt; value)
    { ptr = value.ptr; }

    public void Dispose()
    {
        if (handles.ContainsKey(ptr))
        {
            GCHandle gc = handles[ptr];
            gc.Free();
            handles.Remove(ptr);
            ptr = IntPtr.Zero;
        }
    }

    public T? this[int index]
    {
        get
        {
            if (ptr == IntPtr.Zero) return null;
            if (index &lt; 0) throw new IndexOutOfRangeException();

            if (handles.ContainsKey(ptr))
            {
                GCHandle gc = handles[ptr];

                if (gc.Target is Array) return ((T[])gc.Target)[index];

                return (T)gc.Target;
            }

            return null;

        }
        set
        {
            if (index &lt; 0) throw new IndexOutOfRangeException();
            // not yet implemented
        }
    }

    private T[] getArray()
    {
        if (handles.ContainsKey(ptr)) return (T[])handles[ptr].Target;

        return null;
    }

    public int Count
    {
        get
        {
            if(handles.ContainsKey(ptr))
            {
                GCHandle gc = handles[ptr];
                if (gc.Target is Array) return ((T[])gc.Target).Length;

                return 1;
            }

            return 0;
        }
    }

    public static implicit operator IntPtr(IntPtr&lt;T&gt; value) { return value.ptr; }

    public static implicit operator T(IntPtr&lt;T&gt; value) { return (T)value[0]; }

    public static implicit operator T[](IntPtr&lt;T&gt; value) { return value.getArray(); ; }

    public static implicit operator T?(IntPtr&lt;T&gt; value) { return value[0]; }

}
</code></pre>

<p>It's not complete yet but for now it works, the problem is i keep track of <code>GCHandle</code> by storing them in <code>handles</code> now i need to free the <code>GCHandle</code> once it no more needed so i have to declare a <code>destrcutor</code> but c# don't allow <code>struct</code> to have <code>destrcutor</code> or to override 'Finalize' method and if the such variable of type <code>IntPtr&lt;T&gt;</code> goes out of scope the destruction take place but the <code>GCHandle</code> won't be free.</p>

<p><strong>UPDATE</strong></p>

<p>As an example of this class usage, suppose we going to interpo <code>COAUTHIDENTITY</code> and <code>COAUTHINFO</code> from <code>COM</code>, here what it will look like:</p>

<pre><code>[StructLayout(LayoutKind.Sequential)]
struct COAUTHIDENTITY
{
    [MarshalAs(UnmanagedType.LPWStr)] string User;
    uint UserLength;
    [MarshalAs(UnmanagedType.LPWStr)] string Domain;
    uint DomainLength;
    [MarshalAs(UnmanagedType.LPWStr)] string Password;
    uint PasswordLength;
    uint Flags;
}

[StructLayout(LayoutKind.Sequential)]
struct COAUTHINFO
{
    uint dwAuthnSvc;
    uint dwAuthzSvc;
    [MarshalAs(UnmanagedType.LPWStr)] string pwszServerPrincName;
    uint dwAuthnLevel;
    uint dwImpersonationLevel;
    IntPtr&lt;COAUTHIDENTITY&gt; pAuthIdentityData;
    uint dwCapabilities;
}
</code></pre>

<p>Instead to make <code>pAuthIdentityData</code> an <code>IntPtr</code> and use <code>Marshal</code> member functions to get object of type <code>COAUTHIDENTITY</code>, <code>IntPtr&lt;T&gt;</code> will make it more simple.</p>

<p>The question is: where should i write the code to free <code>GCHandle</code> when the <code>IntPtr&lt;T&gt;</code> is released?</p>
