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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:36:42+00:00 2026-06-09T08:36:42+00:00

I’m trying to figure out how I can make a Generics call take a

  • 0

I’m trying to figure out how I can make a Generics call take a variable for the Type. In the call below it take a type “DAL.Account” and works fine.

var tst = ctx.GetTable<DAL.Account>().Where(t => t.Sbank == "000134");

I want to change that so that I can pass a variable in place of the “DAL.Account”. Something like this but I know that won’t work as you can’t pass property as a Type.

ctx.GetTable<Criteria.EntityType>().Where(LinqToSQLHelper.BuildWhereStatement(Criteria.StateBag), Criteria.StateBag.Values.ToArray())

Below is the shell pieces of code I think explains what I’m trying to do. Generics is not my strong suit so I’m looking for some help. Is there anyway that I can make this happen?

//Stores a "Type" that indicates what Object is a Criteria for.
public class AccountCriteria : IGeneratedCriteria
{
    ...

    public Type EntityType
    {
        get {return typeof(DAL.Account);}
    }
}

//I have added a function to the DataContext called "GetTable"
// And then used it as an example in a Console App to test its functionality.
public class ADRPDataContext : NHibernateDataContext
{
    ...

    public CodeSmith.Data.NHibernate.ITable<T> GetTable<T>() where T : EntityBase
    {
         var tb = new CodeSmith.Data.NHibernate.Table<T>(this);
         return tb;
    }
}

// console application that uses DataContext.GetTable
class Program
{
    static void Main(string[] args)
    {
        using (var ctx = new ADRPDataContext())
        {
            var tst = ctx.GetTable<DAL.Account>().Where(t => t.Sbank == "000134");
        }
    }
}

//ExistsCommand class that uses the EntityType property of the Critera to generate the data.
public class ExistsCommand
{
    private IGeneratedCriteria Criteria { get; set; }

    protected override void DataPortal_Execute()
    {
        using (var ctx = new DC.ADRPDataContext())
        {
            //This was my first attempt but doesn't work becuase you can't pass a property in for a Type.
            //But I can figure out how to write this so that it will work.
            Result = ctx.GetTable<Criteria.EntityType>().Where(LinqToSQLHelper.BuildWhereStatement(Criteria.StateBag), Criteria.StateBag.Values.ToArray()).Count() > 0;
        }
    }
}
  • 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-09T08:36:44+00:00Added an answer on June 9, 2026 at 8:36 am

    You are looking to instantiate a generic type. Some info can be found here

    This is a simple example demonstrating how to instantiate a List with a capacity of 3. Here is a method that you can call to create a generic when you don’t know the type:

    public static Object CreateGenericListOfType(Type typeGenericWillBe)
        {
            //alternative to the followin:
            //List<String> myList = new List<String>(3);
    
    
            //build parameters for the generic's constructor (obviously this code wouldn't work if you had different constructors for each potential type)
            object[] constructorArgs = new Object[1];
            constructorArgs[0] = 3;
    
    
            //instantiate the generic.  Same as calling the one line example (commented out) above. Results in a List<String> with 3 list items
            Type genericListType = typeof(List<>);
            Type[] typeArgs = { typeGenericWillBe };
            Type myNewGeneric = genericListType.MakeGenericType(typeArgs);
            object GenericOfType = Activator.CreateInstance(myNewGeneric, constructorArgs);
    
    
            return GenericOfType;
        }
    

    And here is some sample code that will show you the example method works:

    List<String> Strings = (List<String>)InstantiateGenericTypeWithReflection.CreateGenericListOfType(typeof(String));
    
            //demonstrate the object is actually a List<String> and we can do stuff like use linq extensions (isn't a good use of linq but serves as example)
            Strings.Add("frist");
            Strings.Add("2nd");
            Strings.Add("tird");
            Console.WriteLine("item index 2 value: " + Strings.Where(strings => strings == "2").First());
    

    In your example, replace your GetTable<Criteria.EntityType>() with CreateGenericTableOfType(Criteria.EntityType). This will return a generic table of whatever type you pass in. You will of course need to implement the method properly (handle constructor args, change List to Table etc).

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.