Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must have a public parameterless constructor, it doesn't?

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??

like image 870
bevacqua Avatar asked Apr 14 '11 00:04

bevacqua


2 Answers

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().

like image 126
Ben Voigt Avatar answered Sep 30 '22 18:09

Ben Voigt


you can't instantiate an abstract class

like image 44
clyc Avatar answered Sep 30 '22 18:09

clyc