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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:45:50+00:00 2026-06-05T06:45:50+00:00

A while back I wanted to create my own data mapper that would be

  • 0

A while back I wanted to create my own data mapper that would be much simpler than your average ORM. In doing so I found the need to have access to the type information of inheriting classes in my base class. My first thought was reflection, but it’s too slow (if you use reflection though, check out Fasterflect as it ‘almost’ eliminates the performance problems of reflection).

So I turned to a solution that I later found out had it’s own name: The Curiously Recurring Template Pattern. This mostly solved my problem, but learning how to correctly implement this pattern was a little challenging. The two main questions I had to solve were:

1) How can I let my consuming code work with my generic objects without needing to know the generic parameters the objects were created with?

2) How can I inherit static fields in C#?

The challenging part was actually figuring out the questions. Once I realized what I needed to do, solving these questions was pretty easy. If you find yourself in need of the CRTP, you will likely find yourself needing to answer these questions… they seem to go hand in hand.

  • 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-05T06:45:51+00:00Added an answer on June 5, 2026 at 6:45 am

    Working with generics without knowing the generic parameter types

    When using the CRTP it’s good to have a non-generic base class (abstract if possible, but that’s not too important) that your ‘base’ generic class inherits from. Then you can make abstract (or virtual) functions on your non-generic base class and allow consuming code to work with your objects without having to know the generic parameters. For example:

    abstract class NonGenBase
    {
        public abstract void Foo();
    }
    
    class GenBase<T>: NonGenBase
    {
        public override void Foo()
        {
            // Do something
        }
    }
    

    Now consuming code that has no knowledge of what T is supposed to be can still call the Foo() procedure on your objects by treating them as instances of the base class.

    How to solve the static field inheritance problem

    When using the CRTP to solve a problem, it’s often beneficial to provide access to static fields in inheriting classes. The problem is that C# doesn’t allow inheriting classes to have access to those static fields, except through the type name… which often seems to defeat the purpose in this situation. You may not be able to think of a clear example of what I’m talking about and explaining one is beyond the scope of this answer, but the solution is simple so just tuck it away in your knowledgebase and when you find a need for it you’ll be glad it’s there 🙂

    class GenBase<T>: NonGenBase
    {
        static object _someResource;
    
        protected object SomeResource { get { return _someResource; } }
    }
    

    This ‘simulates’ inheritance of static fields. Keep in mind, however, that static fields on a generic class are not scoped across all your generic implementations. Each generic implementation has its own instance of the static field. If you want a single static field that is available to all the implementations, then you simply need to add it to your non-generic base class.

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

Sidebar

Related Questions

A while back I wrote an ORM layer for my .net app where all
The title, while long, pretty much says it all. What I have is a
Basically, I've wanted to get back into Python, so I decided to make a
I have a button that loads more data into a datagrid when clicked. To
I just ran across a problem that took a little while to figure out
I'm trying to get some code going that lets me display raw trackpad data
I am disabling a UITableView while displaying a UIActivityIndicatorView, and I just wanted to
I wanted to save a string to a file and read it back, so
I'm back with another question concerning threads and synchronization. Imagine a server application that
I came across a situation where I would like to step back through the

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.