结构和析构函数

问题描述:

我创建的类型IntPtr<T>表现得像一个普通指针的C/C++有:结构和析构函数

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

    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<T> 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 < 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 < 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<T> value) { return value.ptr; } 

    public static implicit operator T(IntPtr<T> value) { return (T)value[0]; } 

    public static implicit operator T[](IntPtr<T> value) { return value.getArray(); ; } 

    public static implicit operator T?(IntPtr<T> value) { return value[0]; } 

} 

它尚未完成,但现在它的工作原理,问题是我通过存储保持GCHandle跟踪他们handles现在我需要释放GCHandle一旦没有更多的需要的,所以我必须声明一个destrcutor但是C#不允许structdestrcutor或重写“终结”的方法,如果IntPtr<T>类型的这种变量超出破坏发生的范围,但GCHandle将不会免费。

UPDATE

由于这类用途的例子,假设我们打算从COM interpo COAUTHIDENTITYCOAUTHINFO,这里将是什么样子:

[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<COAUTHIDENTITY> pAuthIdentityData; 
    uint dwCapabilities; 
} 

反而使pAuthIdentityDataIntPtr并使用Marshal成员函数来获取COAUTHIDENTITY类型的对象,IntPtr<T>将使其更简单。

现在的问题是:我应该在IntPtr<T>发布时,在哪里写代码以释放GCHandle

+1

只是...为什么?你在这里试图做什么?既然你已经限制了'T:struct',你已经有了基于范围的生命周期...?或者如果它*是一个类,那么假设(从这里的用法来看)它是短暂的,它将被收集在第0代*反正*中,这是非常便宜的。 –

+0

嗯...这个班应该是类似于IntPtr的类型吗? IntPtr不会跟踪生命周期,它只是一个像C指针一样的指针。 – Skurmedel

+1

参见上面的示例 –

您正在重新发明轮子。看看SafeHandle课程。使用现有的后代或创建自己的后代。

+1

SafeHandle是一个操作系统句柄,不是指向任何数据的指针。 –

+1

@穆罕默德:好点。你可能想看看[GCHandles的真相](http://blogs.msdn.com/b/clyon/archive/2005/03/18/398795.aspx)有关GCHandles和拳击的风险。 – TrueWill