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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:18:05+00:00 2026-05-23T11:18:05+00:00

Somewhere deep inside a library in which I do lots of metaprogramming, there is

  • 0

Somewhere deep inside a library in which I do lots of metaprogramming, there is a class that contains the following code:

// Initialized in the constructor
// Destroyed in IDisposable.Dispose
private IDataReader reader;

private T Retrieve<T>()
    where T : class, new() {

    if (!this.reader.Read())
        return null;

    // This does the same as typeof(T).GetProperties(), but ReflectionHelper
    // memoizes the properties of the types passed to GetProperties.
    PropertyInfo[] properties = ReflectionHelper.GetProperties(typeof(T));

    T result = new T();
    foreach (PropertyInfo property in properties)
        property.SetValue(result, this.reader[property.Name], null);
    return result;
}

public T RetrieveObject() {
    T result = this.RetrieveObject<T>();
    this.reader.NextResult();
    return result;
}

public List<T> RetrieveList<T>()
    where T : class, new() {

    List<T> result = new List<T>();
    for (T element; (element = this.Retrieve<T>(reader)) != null; )
        result.Add(element);
    this.reader.NextResult();
    return result;
}

Needless to say, the class is a wrapper around IDbConnection, IDbCommand and IDataReader, with the added benefit of allowing sane access to the results.

I want to add the following methods to the class:

public T RetrieveCompound<T, U>()
    where T : class, IHeader<U>, new()
    where U : class, new() {

    T header = this.RetrieveObject<T>();
    if (header != null)
        header.Details = this.RetrieveList<U>();
    return header;
}

public T RetrieveCompound<T, U, V>()
    where T : class, IHeader<U>, IHeader<V>, new()
    where U : class, new() {

    T header = this.RetrieveObject<T>();
    if (header != null) {
        ((IHeader<U>)header).Details = this.RetrieveList<U>();
        ((IHeader<V>)header).Details = this.RetrieveList<V>();
    }
    return header;
}

public T RetrieveCompound<T, U, V, W>()
    where T : class, IHeader<U>, IHeader<V>, IHeader<W>, new()
    where U : class, new() {

    T header = this.RetrieveObject<T>();
    if (header != null) {
        ((IHeader<U>)header).Details = this.RetrieveList<U>();
        ((IHeader<V>)header).Details = this.RetrieveList<V>();
        ((IHeader<W>)header).Details = this.RetrieveList<W>();
    }
    return header;
}

However, this has a very important pitfall: The interface IHeader<T> happens to be declared in a different assembly, and I do not want to force users of the IDataReader-wrapper class to add a reference to an assembly they might not need.

I know I could solve this using extension methods, but I am using .NET Framework 2.0. Is there another way to get around this problem?

  • 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-23T11:18:06+00:00Added an answer on May 23, 2026 at 11:18 am

    You could add those methods to a derived type. That way, if you don’t need IHeader<T>-related functionality, you don’t need to reference the assembly.

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

Sidebar

Related Questions

Say I have a struct defined somewhere deep in low-level code used all over
I've built a Navigation-based app, but now that I'm knee-deep in the code, I've
I want to open a file that is somewhere deep in my project tree.
I want a hard reference class in my Java code, but, of course, there
Given a form class (somewhere deep in your giant Django app).. class ContactForm(forms.Form): name
Somewhere in my code I have: class aclass { ... function amethod() { $this->dom
$('#content td#foo').show(); $('td#foo').show(); The td is somewhere deep in the content div. What is
Somewhere along the line I picked up the notion that using iframes is 'bad
Somewhere I was reading that - regarding low memory warnings and giving up an
I read somewhere once that the modulus operator is inefficient on small embedded devices

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.