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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:02:06+00:00 2026-06-15T17:02:06+00:00

I have one Generic Method, where we can pass a T as a interface

  • 0

I have one Generic Method, where we can pass a T as a interface type. Method returns a list of data corresponding T type. I have 20-25 same condition for this method how can I optimize logic.

Class implements interface. example Student class implements IStudent interface.

public ObservableCollection<T> GetAll<T>()
    {
        try
        {
            if (typeof(T) == typeof(IStudent))
            {                 
                return GetAll<T, Student>();
            }
            else if (typeof(T) == typeof(IZone))
            {
                return GetAll<T, Zone>();
            }
            else if (typeof(T) == typeof(IEmployee))
            {
                return GetAll<T, Employee>();
            }
            else if (typeof(T) == typeof(ICourse))
            {
                return GetAll<T, Course>();
            }
         }
    }

Here caller pass interface type T and I check type of T. I pass to other function T and class which will return list of T. Other function in base class which I can not change.
Could anyone suggest me something about same.

  • 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-15T17:02:07+00:00Added an answer on June 15, 2026 at 5:02 pm

    You could try and do this through reflection.

    public ObservableCollection<T> GetAll<T>()
    {
        var typeName = typeof(T).FullName;
        var indexOfDot = typeName.LastIndexOf('.');
        var newTypeName = typeName.SubString(0, indexOfDot) + '.' + typeName.SubString(indexOfDot + 1);
        var newType = Type.GetType(newTypeName);
        var methodTypes = new [] { typeof(T), newType };
        var method = GetType().GetMethod("GetAll");
        var typedMethod = method.MakeGenericMethod(methodTypes);
        return (ObservableCollection<T>) typedMethod.Invoke(this, new object[0]);
    }
    

    Not pretty, but should do the job and does is generically.

    The only caveat the current way is that the GetAll method is overloaded by generic parameters, so it may not get you the right method, or fail because there are two. I’ll have a look if the right one can be indicated.

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

Sidebar

Related Questions

I have an interface IAction that has one generic method: public interface IAction {
I have the following generic method for serialising an input object of one type
Is there a rule for knowing when one has to pass the generic type
I have the following method I can pass in a lambda expression to filter
I have a generic method like the one below. And I check that the
I've got a class with many string arrays. I'd like to have one generic
I have a Generic Base Class that I want to allow one of two
I have one drop down list in my page, which contains two options. What
This may have been asked already, but I can't find it, so here goes.
I have a PHP class-- a generic ODBC database interface. While the methods will

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.