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 6803553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:21:30+00:00 2026-05-26T19:21:30+00:00

I could able to create a list of objects by doing like this: using

  • 0

I could able to create a list of objects by doing like this:

using (var session = sessionFactory.OpenSession()) {              
    ICriteria criteria = session.CreateCriteria<Foo>();
    IList<Foo> Foo = criteria.List<Foo>();
}

where Foo is my class.

I wish to write a generic function to return list of object depending on the input Type as shown below:

public IList<T> GetObjList <T> (T obj)
{
    IList<T> list;
    try {
        var sessionFactory = CreateSessionFactory();
        if (sessionFactory == null)
            return null;
        using (var session = sessionFactory.OpenSession()) {
            ICriteria criteria = session.CreateCriteria<T>(); //Error
            list = criteria.List<T>();
        }
    }
    catch (Exception ex) {
        MessageBox.Show(ex.Message);
        return null;
    }
    return list;
}

I’m getting the following compiler error in the line ICriteria criteria = session.CreateCriteria()

The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'NHibernate.ISession.CreateCriteria<T>()'

Please help me to solve this.

  • 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-26T19:21:31+00:00Added an answer on May 26, 2026 at 7:21 pm

    Generic type parameters can be further constrained to limit what can be provided as such parameter. What your error message says, is that NHibernate.ISession.CreateCriteria<T>() requires its type parameter to be constrained to reference type. Since your method doesn’t define any constraint at all, C# compiler has no clue whether T is a valid type parameter for CreateCriteria<T>.

    If we take a closer look at NHibernate source code, we indeed can notice that ISession.CreateCriteria<T> is declared as follows:

    public ICriteria CreateCriteria<T>() where T : class
    

    As a result, you need the same constraint on your method in order for type parameter passed to it to play nicely with CreateCriteria call. Simply change it to:

    public IList<T> GetObjList<T>(T obj)
        where T : class
    {
        /* ... */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to be able to do this so that I could do something
I would like to create a simple drop down list with static values which
Hai, I have a group of radio buttons, i could able to pick the
I have an app which could benefit from the user being able to choose
Could someone tell me what the units the SetTimeout(int) method in the ICriteria interface
Could somebody please name a few. I could given time, but this is for
If I have a list of elements I would like to sort, Java offers
Requirements: I am building a task list application and wanted tasks to be able
I have a subclass of List<Location> called LocationList . This is a convenient way
I have an abstract class which I would like to be able to expose

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.