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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:38:41+00:00 2026-06-10T13:38:41+00:00

I have an interesting situation where certain things are working but others are not

  • 0

I have an interesting situation where certain things are working but others are not and I’m not sure why. Below is code that approximates my situation. I’ve got a static in a repository that takes a generic type which is implemented by a base type of object. I then have two levels of derived classes based on that generic type. The first level of derived class fills out the generic parameters of the base type and works fine, however any class that derives from the class that filled out the generic parameters does not work in place of the base class it is derived from.

public class Vehicle<TVehicleType, TStorage>
{
}

public class Car : Vehicle<Car, ParkingLot>
{
}

public class PickupTruck : Car
{
}

public class Dealership <TDrivableVehicle>
{
    public static TDrivableVehicle GetVehicle<TVehicleType, TStorage>(TStorage lot)
         where TDrivableVehicle : Vehicle<TVehicleType, TStorage>, new()
    {
    }
}

public class CarDealership : Dealership<Car>
{
    public static Car GetDrivableVehicle(aCarParkingLot)
    {
        return Dealership.GetDrivableVehicle<Car, CarParkingLot>(aCarParkingLot); <-- Works fine
    }
}

public class PickupTruckDealership : CarDealership
{
    public static PickupTruck GetDrivableVehicle(aCarParkingLot)
    {
        return Dealership.GetDrivableVehicle<PickupTruck, CarParkingLot>(aCarParkingLot); <-- fails
    }
}

Certain aspects seem to work correctly in terms of PickupTruck understanding its generic base, but extensions (not shown here) and passing the type to a type parameter specifically do not(the GetDrivableVehicle call). My guess is that the extension method is related to the type parameter issue since it would need to figure out the type.

Any ideas why this doesn’t work and/or what can be done to work around it?

  • 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-10T13:38:43+00:00Added an answer on June 10, 2026 at 1:38 pm

    Having rewritten your code to a point where I can get it to fail where you say it will – the problem is exactly as Thom Smith says: PickupTruck inherits Car and therefore is a Vehicle<Car, ParkingLot>, not a Vehicle<PickupTruck, ParkingLot>. Also, because of it’s generic inheritance it is impossible for it to be anything else other than that.

    I know your code is only a boiled down representation of the problem you’re having – but if it’s close enough, then there might be some useful observations we can make here about the overall architecture.

    I’m not against generic bases being aware of their derived counterparts – indeed it’s particularly useful for factories; however it almost always instantly rules out any further inheritance.

    You’re trying to encode far too much information at the type level; and apart from the number of angle brackets we see here it’s actually hinted at by the slightly incongruous nature that Vehicle<TVehicleType, TStorage> is determining the type of storage that it can be stored within.

    To me, that just doesn’t make any sense because let’s say we have a ParkingLot today, but tomorrow we also get a Hangar (for cars being stored under cover) – this will require a whole new swathe of vehicle types which are unequal by virtue of the fact that we also have the derived type being passed to the base Vehicle<TDerived, ...> – ergo ParkingLotCar and HangarCar can never be equivalent, even if two instances both represent the same make/model etc.

    So, anticipating that, you have gone for inheritance where you have a common Car, but of course at that point any inheritance is pointless because Car is a Vehicle<Car,...> so anything derived from it must also be. Only with Multiple inheritance could that not be the case, but even with that it doesn’t get around the whole ParkingLot question.

    Ask yourself why does Vehicle<,> need to know about the deriving type? Is it so you can have a single factory method? In that case, you should put it into the Dealership, or the ParkingLot types; not the Vehicle base:

    public interface IVehicle {}
    public interface ICar : IVehicle {} //because pickup trucks share some car traits
    public interface IPickup : ICar, IVehicle {}
    public interface IStorage {}
    
    public class Car : ICar, IVehicle {}
    public class Pickup : IPickup, ICar, IVehicle {}
    
    public class ParkingLot : IStorage {}
    public class Hangar : IStorage {}
    
    public class Dealership
    {
      public static TVehicle GetVehicle<TVehicle>(IStorage storage)
        where TVehicle : IVehicle, new()
      {
    
      }
    }
    
    //now you can specialise if you really need to
    
    public class CarDealership
    {
        public static Car GetVehicle(IStorage storage)
        {
            return Dealership.GetVehicle<Car>(storage);
        }
    }
    
    public class PickupDealership
    {
        public static Pickup GetVehicle(IStorage storage)
        {
            return Dealership.GetVehicle<Pickup>(storage);
        }
    }
    

    Now you have the runtime relationships between your vehicle types; allowing different concrete types to share traits such as those on the ICar or IPickup interfaces; but you’ve broken the relationship between the vehicle and it’s storage so you can get an IVehicle from a FootballPitch or drive it off the pier to the RiverBed if you need to.

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

Sidebar

Related Questions

I have an interesting situation, well it's probably not that unique at all, but
I have an interesting situation on my hands, and I'm not quite sure how
Interesting situation. I have a section of code that creates several ZipOutputStreams. As a
I have an interesting situation that EMF forced me into: abstract class AbstractDog{ ...
We've run into an interesting situation that needs solving, and my searches have turned
I'm working with vector now and I have an interesting situation which I need
I have this interesting situation. I have a bunch of structs that hold a
I have an interesting situation that has me stumped. It seems that posting appliction/json
Hi folks hi have a very interesting situation. i have a viewmodel that requires
I have an interesting situation. In some (large) legacy code, there is a namespace

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.