Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1102927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:15:44+00:00 2026-05-17T01:15:44+00:00

I have a C# .NET 2.0 CF project where I’m using a number of

  • 0

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:

  1. How do I define an abstract interface for static functions?
  2. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:15:45+00:00Added an answer on May 17, 2026 at 1:15 am

    Yes, can’t work. You need the actual instance of a class that implements IHandleTraits. Interfaces need a class that implements a v-table. As long as you want to seal this class, you need a factory or create an instance of T.

    Realistically, the object hierarchy is wrong. SafeHandle should be an abstract base class with concrete derived classes that each know how to deal with a specific handle type. That’s how the desktop version of SafeHandle is implemented as well.

    You can rescue your approach by giving T the new() constraint so that you can create an instance of it. Dragging around the extra reference isn’t that great though:

    public sealed class SafeHandle<T> : IDisposable where T : IHandleTraits, new() {
        private bool disposed;
        private IntPtr handle_ = IntPtr.Zero;
        private T handleType;
        public SafeHandle() {
            handleType = new T();                        
            handle_ = handleType.Create();
        }
        // etc...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .net project (MySolution.Common) that uses the app.config. I am using the
I have a vs.net project, and after some refactoring, have modified the name of
I have a .net 1.1 project in VS.Net 2003 (using C#) in which I
I have a .net project that has a web reference to a service. I
I have a .NET project which references another assembly that is built outside of
I have a CC.NET project configured to call a common NAnt build file, which
I have a .net interop project that uses an app.config file. When I am
I have an ASP.net project I'm looking at and they want to use MySQL.
I have a ASP.NET project, in which I have a method with 10 local
I have an ASP.NET MVC project with a form. In the Action method that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.