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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:33:14+00:00 2026-05-17T02:33:14+00:00

SHORT FORM(!) In response the Jon Skeet’s comment, what I want C# to be

  • 0

SHORT FORM(!)

In response the Jon Skeet’s comment, what I want C# to be to do is to allow a generic, at compile time, to expand out and derive from one of it’s generic parameters (as Kirk Woll demonstrates):

public class Generic<TBase> : TBase {

}

This class, of course, would not be able to override any members of TBase (unless perhaps if a constraint was applied) and would have to repeat all of TBase’s public + protected constructors untouched – automatically forwarding the calls to the base.

The long form of the question shows an abstract + concrete class that presents a common pattern for implementing a given interface – but which, because C# doesn’t allow multiple inheritance, cannot easily be applied on top of another base class without cloning all of the code and sticking a base class under the abstract – not exactly DRY!

LONG FORM

I know the technical reasons why the following code doesn’t work – generics are run-time compiled, not compile-time templates – but I’m curious to know why the C# designers appear so reticent to go this way with the language (i’ve seen readability cited but struggle to understand that). I’d also like to know how you guys would implement this pattern.

I have an interface for an object that resolves dependencies via an IDependencyResolver:

public interface IDependant
{
  IDependencyResolver Resolver { get; set; }
}

I then have an abstract class

public abstract class DependantBase : IDependant
{
  #region IDependant Members

  public abstract IDependencyResolver Resolver
  {
    get; set; 
  }

  #endregion

  public virtual TDependency ResolveDependency<TDependency>(
    string name, params object[] args)
  {
    //null checks elided
    return Resolver.Resolve<TDependency>(name, args);
  }

  public TDependency ResolveDependency<TDependency>(
    params object[] args)
  {
    return ResolveDependency<TDependency>(null, args);
  }
}

And finally an instance class that simply materialises the Resolver property:

public class Dependant : DependantBase
{
  public override IDependencyResolver Resolver
  { get; set; }
}

So I can derive this type and override both the Resolver property, as well as how the resolve operation is actually carried out.

I’ve used it on a vanilla type (i.e. one that otherwise doesn’t have a base) and then I come to write an MVC controller.

What I want to be able to do is to repeat the above two classes, but have them implemented as follows:

//'injects' the DependantBase functionality
//between TBase and the deriving class
public abstract class DependantBase<TBase> : TBase, IDependant
{
  //as DependantBase above
}

//and then have Dependant<TBase>:
public class Dependant : DependantBase<TBase>
{ 
  //as Dependant above
}

Thus, if the above were possible I could simply do this:

public class HomeController : Dependant<System.Web.Mvc.Controller>
{

}

And now HomeController derives from Dependant<System.Web.Mvc.Controller>, which in turn inherits from System.Web.Mvc.Controller.

The key here being that I want to preserve the previous pattern of being able to override both the property and the ResolveDependency helper method – but I don’t want to have to keep cloning the same infrastructure code for each new branch of types (i.e. where a new pre-existing base type is required by some framework).

This is clearly not a generic in the .Net term because I’m not sure how you’d even begin to represent this in metadata – but the C# compiler could certainly treat this is as a template instead and expand it out at compile-time (god, it does it enough of this already with Expression Trees, DLR interfacing, in-line delegates, iterator blocks and auto get/set properties!).

The only way I can see of doing something like this is to push it up into a Visual Studio plugin (new language extension or something like that), or even a text template.

  • 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-17T02:33:15+00:00Added an answer on May 17, 2026 at 2:33 am

    You could potentially accomplish your goals by using T4. This is already built into Visual Studio, and works with C# today.

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

Sidebar

Related Questions

Is the second query a short form of the first? Is the second one
i want to implement a sort of feedback form/survey form in asp.net which is
In a sort of try/catch form I want to execute a bash that doesn't
I'm having a little problem at the moment :( I've got a short form
I am making some sort of form with many input fields (some are textfields,
I'm trying to create an Advanced Searching form that sort of look like this
I have a form with some controls. (Actually, this is a sort of ongoing
Short question, is the following ok: struct X { A& x; A y; X()
Short one: Am I correct in assuming, that the Samsung Galaxy S wifi 4.0
Short version: Can I grant access to external databases to a role? Long version:

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.