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

  • SEARCH
  • Home
  • 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 8506139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:30:49+00:00 2026-06-11T02:30:49+00:00

I have the following interfaces defined in a base library project: public interface IWorkContext

  • 0

I have the following interfaces defined in a base library project:

public interface IWorkContext {
    T User<T>() where T : IUser;

    // Note this method is kind of a shortcut so i don't
    // have to keep passing in T
    IUser User();
}

public interface IUser {
    int Id { get; }
    string UserName { get; }
}

Now I have another project which references the one above with the following class (which implements IUser).

public class User : IUser {
   public virtual int Id { get; set; }
   public virtual string UserName { get; set; }
   // ... Code removed for brevity
}

Now i also need to implement IWorkContext. Here’s my first attempt:

public class DefaultWorkContext : IWorkContext {
    private readonly IUsersService _usersService;

    public DefaultWorkContext(IUsersService usersService) {
        _usersService = usersService;
    }

    public T User<T>() where T : IUser {
        return _usersService.GetUser<T>(1));
    }

    public User User() {
        return User<User>();
    }
}

However this gives the error:

The type ‘T’ cannot be used as type parameter ‘T’ in the generic type
or method ‘…GetUser(int)’. There is no boxing conversion or type
parameter conversion from ‘T’ to ‘User’.

I’m sure there’s something fundamental i’m doing wrong. I’d appreciate any advice on this model can be improved. Thanks

Edit (as requested here’s the GetUser method):

public T GetUser<T>(int id) where T : User {
    return _session.Get<T>(id);
}

Note: _session is an NHibernate session.

  • 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-06-11T02:30:51+00:00Added an answer on June 11, 2026 at 2:30 am

    The problem here is that you are passing a generic type as a type parameter to your GetUser() method, that may not satisfy the condition of inheritance of the generic type accepted by your GetUser() method.

    The compiler ensures that the type passing as parameters always satisfy the type conditions.
    Indeed, you could write a class NotUser that implements IUser while not being a User.
    Your User<T>() method could then be called with NotUser as the type parameter, and that would result in a call to GetUser() with a type parameter that does not inherit from User.

    Your GetUser() method only accept a type that inherits from User.

    You need to either change GetUser() prototype to accept a generic type that implements IUser, or the User<T> prototype to accept only generic types that inherits from User.

    Either this:

    public T GetUser<T>(int id) where T : IUser {
        return _session.Get<T>(id);
    }
    

    or this:

    public T User<T>() where T : User {
        return _usersService.GetUser<T>(1));
    }
    

    EDIT:
    Whether you should pick the first or the second method depends on what you want to do.

    1) You want your IWorkContext to work with the IUser interface, and you accept that the GetUser() method returns a IUser => pick option 1

    2) You want the GetUser() method to take a type that inherits from User, and you accept that the IWorkContext interface works with User instead of IUser => pick option 2, and modify the IWorkContext interface.

    To me, option 1) is better, as you don’t need to modify the interface and you don’t break the genericity. But it depends on what you want to do exactly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following interfaces defined: public interface IAudit { DateTime DateCreated { get;
I have defined the following Interface [ServiceContract] public interface IHealthProducts { [OperationContract()] ResponseClass OrderSelfSignedHealthCertificate();
I have the following Interfaces: public interface ITemplateItem { int Id { get; set;
I have the following two interfaces: public interface IMembershipProvider { object Login(ILoginProviderParameters loginParameters); void
Let's say I have 2 interfaces defined like so: public interface ISkuItem { public
I have defined the following interface: public interface IHaveAProblem { string Issue { get;
I have the following base abstract class defined as: public abstract class BaseObject<T> :
I have the following interface defined to expose a .NET class to COM: [InterfaceType(ComInterfaceType.InterfaceIsDual)]
In my project I have the following three interfaces, which are implemented by classes
I currently have interfaces much like the following: interface IService { void Start(); IHandler

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.