开发者

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 ha开发者_StackOverflowve 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??


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


you can't instantiate an abstract class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜