16
votes
public interface IAutomatizableEvent
{
    Event AutomatizableEventItem { get; }
    bool CanBeAutomatic { get; }
    bool IsAutomaticallyRunning { get; }

    bool OnBeforeAutomaticCall();
    bool OnAutomaticCheck();
    void OnAutomaticStart();
    void OnAutomaticCancel();
}

public abstract class AutomatizableEvent : IAutomatizableEvent
{
    public AutomatizableEvent()
    {
    }

    #region Implementation of IAutomatizableEvent

    public abstract Event AutomatizableEventItem { get; }
    public abstract bool CanBeAutomatic { get; }
    public abstract bool IsAutomaticallyRunning { get; }
    public abstract bool OnBeforeAutomaticCall();
    public abstract bool OnAutomaticCheck();
    public abstract void OnAutomaticStart();
    public abstract void OnAutomaticCancel();

    #endregion
}



public static class EventSystem
{
    public static List<EventSystemBase<Event, AutomatizableEvent>> AutomatizableEvents { get; set; }
    [...]
}

"The type 'AutomatizableEvent' must have a public parameterless constructor in order to use it as parameter 'K' in the generic class 'EventSystemBase'"

public abstract class EventSystemBase<T, K> : AutomatizableEvent
    where T : Event
    where K : AutomatizableEvent, new()

My question is ... doesn't AutomatizableEvent DO HAVE a public parameterless constructor??

2
To satisfy the new() constraint, you must be able to initialize the class with =new AutomatizableEvent() which you can't as it is abstract. Create a concrete child class and use that instead.John Alexiou

2 Answers

35
votes

The error description is wrong, but the error is correct.

AutomatizableEvent cannot be used as generic parameter K because of the constraint where K : new(). An abstract class does not satisfy that constraint.

A constructor of an abstract class is effectively protected, always, since an abstract can only ever be created as a base subobject of a derived class, the constructor can likewise only ever be called by a constructor of a derived class, and only in constructor chaining. Specifically it can't be used by EventSystemBase<T, K> in the context new K().

6
votes

you can't instantiate an abstract class