Click here to Skip to main content
15,921,941 members
Home / Discussions / C#
   

C#

 
SuggestionFYI: FileOptions.WriteThrough / FileStream.Flush(true) / FlushFileBuffers Pin
Sascha Lefèvre15-Oct-17 0:28
professionalSascha Lefèvre15-Oct-17 0:28 
Questionvoipsdk.dll..... basic ivr c# Pin
karimkarimkarim13-Oct-17 14:50
karimkarimkarim13-Oct-17 14:50 
AnswerRe: voipsdk.dll..... basic ivr c# Pin
Richard Andrew x6413-Oct-17 16:38
professionalRichard Andrew x6413-Oct-17 16:38 
QuestionC# multitasking Pin
Member 1346321013-Oct-17 0:51
Member 1346321013-Oct-17 0:51 
AnswerRe: C# multitasking Pin
OriginalGriff13-Oct-17 1:59
mveOriginalGriff13-Oct-17 1:59 
QuestionThread WaitReason.UserRequest Pin
Bernhard Hiller12-Oct-17 21:44
Bernhard Hiller12-Oct-17 21:44 
QuestionC# - run exe from within a form, and keep the exe's borders within the actual boundary of the form... Pin
Member 1184284812-Oct-17 13:38
Member 1184284812-Oct-17 13:38 
AnswerRe: C# - run exe from within a form, and keep the exe's borders within the actual boundary of the form... Pin
Afzaal Ahmad Zeeshan12-Oct-17 16:24
professionalAfzaal Ahmad Zeeshan12-Oct-17 16:24 
GeneralRe: C# - run exe from within a form, and keep the exe's borders within the actual boundary of the form... Pin
BillWoodruff20-Oct-17 16:47
professionalBillWoodruff20-Oct-17 16:47 
GeneralRe: C# - run exe from within a form, and keep the exe's borders within the actual boundary of the form... Pin
Afzaal Ahmad Zeeshan21-Oct-17 1:53
professionalAfzaal Ahmad Zeeshan21-Oct-17 1:53 
AnswerRe: C# - run exe from within a form, and keep the exe's borders within the actual boundary of the form... Pin
lmoelleb12-Oct-17 19:28
lmoelleb12-Oct-17 19:28 
QuestionC# Listview Pin
danmor49812-Oct-17 9:52
danmor49812-Oct-17 9:52 
AnswerRe: C# Listview Pin
Afzaal Ahmad Zeeshan12-Oct-17 16:33
professionalAfzaal Ahmad Zeeshan12-Oct-17 16:33 
GeneralRe: C# Listview Pin
danmor49816-Oct-17 4:56
danmor49816-Oct-17 4:56 
GeneralRe: C# Listview Pin
Simon_Whale17-Oct-17 4:45
Simon_Whale17-Oct-17 4:45 
GeneralRe: C# Listview Pin
danmor49818-Oct-17 6:18
danmor49818-Oct-17 6:18 
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 

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.