I have a C# .NET 2.0 CF project where I'm using a number of native methods. Several of those methods revolve around HANDLE
objects of some sort. I'd like to abstract out the handle management lifetime using generics like this:
public abstract class IHandleTraits
{
public abstract IntPtr Create();
public abstract void Release(IntPtr handle);
}
public sealed class SafeHandle<T> : IDisposable where T : IHandleTraits
{
private bool disposed;
private IntPtr handle_ = NativeMethods.INVALID_HANDLE_VALUE;
public SafeHandle()
{
// error CS0119: 'T' is a 'type parameter', which is not valid in the given context
handle_ = T.Create();
if (NativeMethods.INVALID_HANDLE_VALUE == handle_)
{
// throw win32 exceptions
}
}
~SafeHandle()
{
this.Dispose(false);
}
public IntPtr Handle
{
get { return handle_; }
}
public void Dispose(bool disposing)
{
if (this.disposed)
{
// error CS0119: 'T' is a 'type parameter', which is not valid in the given context
T.Release(handle_);
handle_ = NativeMethods.INVALID_HANDLE_VALUE;
}
this.disposed = true;
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}
Now, I could define a traits structure for each handle type like this:
internal sealed class MyHandleTraits : IHandleTraits
{
// error CS0112: A static member cannot be marked as override, virtual, or abstract
public static override IntPtr Create()
{
return NativeMethods.CreateHandle();
}
// error CS0112: A static member cannot be marked as override, virtual, or abstract
public static override void Release(IntPtr handle)
{
NativeMethods.ReleaseHandle(handle);
}
}
And use it in the application like this:
using (SafeHandle<MyHandleTraits> MyHandle = new SafeHandle<MyHandleTraits>)
{
// execute native methods with this handle
}
Obviously, this has several problems:
- How do I define an
abstract
interface forstatic
functions? - When I try to use the generic, I get an error that says it's a "type parameter" which is "not valid in the given context".
What can I do to solve or work around these issues?
Thanks, PaulH