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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:49:15+00:00 2026-05-29T14:49:15+00:00

Additional information I appreciate the attempts to redefine the problem but the structure in

  • 0

Additional information

I appreciate the attempts to redefine the problem but the structure in question is non negotiable. I’m working with a code base created in 2004 in .NET 1.1 before generics and a lot of other nice features. I cannot break the existing model – only enhance it (hence the conversion of C to C – in totality its actually C : C where C is maintained only to preserve existing inheritance dependencies.

Our 8 year old data layer is a poor man’s ORM – it encapsulates Entity functionality (ie: the table fields) and data functionality (connecting and pulling data) in one class. Abstract Generic C and abstract B are generated by a utility app we have used since 2004. That’s why B is abstract – so developers are forced to think about the real instantiation of B as type A. (Which was not fully successful, admittedly)

There are over 100 tables in this model, and so over 100 “A” classes in our projects. I cannot adopt any change that will require every single A class to be manually updated. Any changes I make must be done in a generated fashion – and ideally as low down the inheritance root as possible.

The existing select methods return data sets, not themselves. If I am to improve them at all it is only to make them use data readers and return instances of and lists of themselves.

SO thank you to those who’ve replied so far, but what would actually help me here is an answer to the problem presented, not attempts to redefine the problem to suit what would be ideal in a new app.


Original content:

I have a base abstract generic class C

public abstract class C<T>

from it is derived an abstract class B

public abstract class B : C<B>

And finally A instantiates B

public class A : B

B has methods which return a list of B, that needs to be able to invoke an instance of C to populate the list. I want the method to instance this 2nd generation descendent to reside in C.

public abstract class C<T>
{
    protected abstract PopulateList(List<T> theList, SqlDataReader dr);
    protected TI GetNew<TI>() where TI:T, new() {return new TI();}
}

SO far so good, but now in B I need the PopulateList method to be able to instance new A objects to fill them from a datareader that provides data common to B

public abstract class B : C<B>
{
    public int MyInt {get;set;}

    protected override void PopulateList(List<B> DistributionList, SqlDataReader dr)
    {
        while (dr.Read())
        {
            Distribution newOne = GetNew<B>();

            newOne.MyInt = (Int32)dr["MyInt"];
            DistributionList.Add(newOne);
        }
    }
}

So do I need to make B generic as well? B<T> : C<T> where T:B and reproduce populate list in the same manner as GetOne()? Or is there some way I can force PopulateList to invoke new instances of the current type derived from B without adding a new layer of generics?

Not that a new layer of generics is bad per se- just not preferred in this instance

  • 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-29T14:49:17+00:00Added an answer on May 29, 2026 at 2:49 pm

    You can achieve this without more generics if you’re willing to make this change to GetNew() in class C<T>:

    protected abstract T GetNew();
    

    In B.PopulateList:

    B newOne = GetNew();
    

    And in class A:

    protected override B GetNew()
    {
        return new A();
    }
    

    UPDATE: If it isn’t practical to make any changes to class A, then you can instead define GetNew() in C<T> as follows:

    protected T GetNew()
    {
        return (T)Activator.CreateInstance(this.GetType());
    }
    

    Inelegant, yes, but it gets the job done.

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

Sidebar

Related Questions

I need to display additional information, like a tooltip, but it's a lot of
In C#, will the folloing code throw e containing the additional information up the
how do I pass additional information to the service method returning the collection of
I want to store additional information on my users (address, phone). Should I extend
Given a credit card number and no additional information, what is the best way
What would be the best way to store additional information outside of active directory?
I'm storing some additional per-user information using the AUTH_PROFILE_MODULE . We can access the
Additional questions regarding SilentGhost's initial answer to a problem I'm having parsing Twitter RSS
Additional thanks extend to Daniel Newby for answering my memory usage question (and Martin
Though the question is very specific, I'd also really appreciate general advice and other

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.