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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:56:28+00:00 2026-05-22T16:56:28+00:00

I have the following class: public class GenericClass<T> : IGenericClass<T> where T : class

  • 0

I have the following class:

public class GenericClass<T> : IGenericClass<T> where T : class
{
    public GenericClass()

    public GenericClass(Entity e)

    public IQueryable<T> GenericMethod1()

    public IEnumerable<T> GenericMethod2()

    public T NonGenericMethod1(T t)
}

The class works great; however I’m starting to run into issues where I have to instantiate another instance of GenericClass for every type T I want to use, and it’s getting a little crazy. Is there some sort of abstraction I can create to simplify this?

I was heading in this direction, but I can’t tell if this is the right choice or if there is a better design pattern I could use; plus, the two invoke calls are not working correctlly at all.

public class TestClass
{
    private Type type;

    public object Invoke(string method, object obj)
    {
        type = obj.GetType();

        MethodInfo m = typeof(GenericClass<>).GetMethod(method);

        var result = new object();

        if(m.IsGenericMethod == true)
            result = m.MakeGenericMethod(type).Invoke(null, new object[] { obj });
        else
            result = m.Invoke(null, new object[] { obj });

        return result;
    }
}

TIA

  • 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-22T16:56:29+00:00Added an answer on May 22, 2026 at 4:56 pm

    however I’m starting to run into issues where I have to instantiate another instance of GenericClass for every type T I want to use, and it’s getting a little crazy

    It’s hard to guess without some implementation of GenericClass… but I see constructors and methods – no properties (and no fields?).

    If that’s the case, you may want to make GenericClass a static class with static methods. Then you aren’t allowed to instantiate it and you can call the methods directly from the type:

    public static class GenericClass
    {
      public static IQueryable<T> GenericMethod1<T>() where T:class
    
      public static IEnumerable<T> GenericMethod2<T>() where T:class
    
      public static object NonGenericMethod1(object t)
    }
    

    Called by

    IQueryable<Customer> query = GenericClass.GenericMethod1<Customer>();
    IEnumerable<Customer> items = GenericClass.GenericMethod2<Customer>();
    Customer c = (Customer) GenericClass.NonGenericMethod1(customerInstance);
    

    Or perhaps there are properties or fields, but they aren’t dependent on T, then you can move the Generic responsibility to the methods instead of the class.

    Now you can have an instance, and that instance can handle all of the T’s you want to throw at it.

    public class GenericClass : IGenericClass
    {
      public IQueryable<T> GenericMethod1<T>() where T:class
    
      public IEnumerable<T> GenericMethod2<T>() where T:class
    
      public object NonGenericMethod1(object t)
    }
    

    I apologize for the generic-ness of this answer, however that is due to the generic-ness of the question.

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

Sidebar

Related Questions

I have following class: class A { public IEnumerable Song { get; set; }
Suppose I have the following class: public class GenericClass<T> { public T Find() {
I have the following: GenericClass<T> : Class { T Results {get; protected set;} public
I have a generic class and a derived class as following. public class GenericClass<T>
I have following class ( entity ) @Entity public class Magazine { private int
I have the following generic class: public class MessageProcesser<T> where T : Message Inside
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class (as seen through reflector) public class W : IDisposable {
I have the following class public class MenuVeiculo { public string Nome { get;
I have the following class public class UIControl { public string FName{ get; set;

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.