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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:57:23+00:00 2026-05-23T04:57:23+00:00

I have a generic method: (simplified) public class DataAccess : IDataAccess { public List<T>

  • 0

I have a generic method: (simplified)

public class DataAccess : IDataAccess
{
    public List<T> GetEntity<T>()
    {
       return GetFromDatabase<T>(); //Retrieve from database base on Type parameter
    }
}

For testing purpose, I want to create a stub from which I want ‘Foo’ should be returned with some data:

public class DataAccessStub : IDataAccess
{
    public List<T> GetEntity<T>()
    {
       List<Foo> listFoo = new List<Foo>();
       Foo foo = new Foo();
       foo.Name = "Some Name";
       listFoo.Add(foo);

       return listFoo; // I want Foo to be returned
    }
}

Since T has not identified what type it is yet, I can’t return List<Foo>. Compiler error will occur. So How can I write a stub for this kind of generic method?

Edit: Changed the code a little bit. The first method will retrieve from the database base on Type Parameter. The second is a stub for testing.
Sorry that I am not sure if this explain what I want to mention.

Thanks.

  • 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-23T04:57:24+00:00Added an answer on May 23, 2026 at 4:57 am
        interface IA
    {
        List<T> Get<T>();
    }
    
    class StubA : IA
    {
        public List<T> Get<T>()
        {
            var item = new Foo();
            var data = new List<Foo> {item};
            return new List<T>(data.Cast<T>());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method to return a group of objects as a generic list
I have a generic list... public List<ApprovalEventDto> ApprovalEvents The ApprovalEventDto has public class ApprovalEventDto
In this simplified example I have a generic class, and a method that returns
I have a generic method like this (simplified version): public static TResult PartialInference<T, TResult>(Func<T,
Anyone have a quick method for de-duplicating a generic List in C#?
I have a generic class in C# with 2 constructors: public Houses(params T[] InitialiseElements)
I have a generic Collection class, with a variety of public getter methods. To
Using SubSonic3, I have this generic method (thanks to linq guy, James Curran): public
There is a generic method that takes a class as parameter and I have
I have a class Exporter which has a generic method which accepts an IEnumerable<T>

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.