Click here to Skip to main content
15,905,508 members
Home / Discussions / C#
   

C#

 
QuestionInheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
mbv80011-Oct-17 11:48
mbv80011-Oct-17 11:48 
QuestionRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
Eddy Vluggen11-Oct-17 12:53
professionalEddy Vluggen11-Oct-17 12:53 
GeneralRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
PIEBALDconsult11-Oct-17 13:17
mvePIEBALDconsult11-Oct-17 13:17 
GeneralRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
Eddy Vluggen11-Oct-17 13:43
professionalEddy Vluggen11-Oct-17 13:43 
AnswerRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
mbv80012-Oct-17 1:50
mbv80012-Oct-17 1:50 
GeneralRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
PIEBALDconsult11-Oct-17 13:30
mvePIEBALDconsult11-Oct-17 13:30 
GeneralRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
mbv80012-Oct-17 5:44
mbv80012-Oct-17 5:44 
AnswerRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
Richard Deeming12-Oct-17 3:50
mveRichard Deeming12-Oct-17 3:50 
It looks like you need to use generic covariance:
Covariance and Contravariance in Generics | Microsoft Docs[^]

You'll need to use the IVehicle<T> interface instead of the Vehicle<T> class, since generic covariance doesn't work with classes.

You'll also need to remove the setter for the actions property, since that would break the rules for covariance.

The changes you need:
C#
public interface IVehicle<out TActions> where TActions : IActions
{
    TActions actions { get; }
}

public abstract class Vehicle<TActions> : IVehicle<TActions> where TActions : Actions
{
    public abstract TActions actions { get; }
}

public class Car : Vehicle<CarActions>
{
    public int Wheels { get; set; }
    public override CarActions actions { get; } = new CarActions();
}

public class Airplane : Vehicle<AirplaneActions>
{
    public override AirplaneActions actions { get; } = new AirplaneActions();
}

...

static void Main(string[] args)
{
    // You can use either Actions or IActions here;
    // but you *MUST* use IVehicle, not Vehicle.
    
    IVehicle<IActions> aCar = new Car();
    IVehicle<Actions> anAirplane = new Airplane();
 
    List<IVehicle<IActions>> list = new List<Vehicle<Actions>>
    {
        aCar,
        anAirplane
    };
            
    foreach (var vehicle in list)
    {
        vehicle.actions.ID = "any";
        vehicle.actions.PerformAction();
    }
    
    Console.WriteLine("Good");
}

The sample still won't do anything useful, but it should get you started. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
mbv80012-Oct-17 5:42
mbv80012-Oct-17 5:42 
GeneralRe: Inheriting generic classes from base classes with generic parameters. Error converting to base type. Pin
PIEBALDconsult12-Oct-17 12:44
mvePIEBALDconsult12-Oct-17 12:44 
QuestionMongoDB File upload Pin
Elias Zaman11-Oct-17 1:10
Elias Zaman11-Oct-17 1:10 
AnswerRe: MongoDB File upload Pin
OriginalGriff11-Oct-17 1:26
mveOriginalGriff11-Oct-17 1:26 
GeneralRe: MongoDB File upload Pin
Elias Zaman12-Oct-17 17:56
Elias Zaman12-Oct-17 17:56 
GeneralRe: MongoDB File upload Pin
OriginalGriff12-Oct-17 19:26
mveOriginalGriff12-Oct-17 19:26 
AnswerRe: MongoDB File upload Pin
Nathan Minier11-Oct-17 1:36
professionalNathan Minier11-Oct-17 1:36 
QuestionDictionary Pin
Member 114992589-Oct-17 11:04
Member 114992589-Oct-17 11:04 
AnswerRe: Dictionary Pin
Mycroft Holmes9-Oct-17 12:55
professionalMycroft Holmes9-Oct-17 12:55 
AnswerRe: Dictionary Pin
Pete O'Hanlon9-Oct-17 20:16
mvePete O'Hanlon9-Oct-17 20:16 
AnswerRe: Dictionary Pin
OriginalGriff9-Oct-17 21:12
mveOriginalGriff9-Oct-17 21:12 
SuggestionRe: Dictionary Pin
Eddy Vluggen11-Oct-17 12:56
professionalEddy Vluggen11-Oct-17 12:56 
GeneralRe: Dictionary Pin
OriginalGriff11-Oct-17 19:47
mveOriginalGriff11-Oct-17 19:47 
AnswerRe: Dictionary Pin
BillWoodruff20-Oct-17 16:49
professionalBillWoodruff20-Oct-17 16:49 
QuestionAdd Authentication to Web MVC API Pin
Member 47739879-Oct-17 11:02
Member 47739879-Oct-17 11:02 
QuestionHow to click one of many buttons with its name of an web page to submit an form? Pin
Carvin Chen9-Oct-17 5:16
Carvin Chen9-Oct-17 5:16 
QuestionGenerate a WPF-Menu based on a XML-document Pin
Member 131005738-Oct-17 3:08
Member 131005738-Oct-17 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.