Antony Scott
Antony Scott

Reputation: 22006

How do I declare the constructor for a generic class with a non-generic base class with parameters

I have a base class which is non-generic with a derived generic class. The AbstractRepository and ILoggingManager are provided by the IoC framework.

Base Class

public abstract class EventHandlerBase
    : AbstractDataProvider
    , IEventHandler
{
    public EventHandlerBase(
        AbstractRepository data,
        ILoggingManager loggingManager
        )
        : base(data, loggingManager)
    {
    }
}

Derived Class

public abstract class EventHandlerBase<TDetail>
    : EventHandlerBase
    where TDetail : Contracts.DeliveryItemEventDetail
{
}

I would like to declare a constructor in the derived class that will call the non-generic base class, but I do not know the syntax.

Upvotes: 1

Views: 3483

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1504122

public class EventHandlerBase(AbstractRepository data,
                              ILoggingManager loggingManager)
     : base(data, loggingManager)
{
     // Whatever
}

should work fine. If it doesn't, could you show the problems you're getting?

EDIT: I think the whole base class thing may be clouding the issue here. When you declare a constructor in a generic type, you don't specify the type parameters. For example, List<T> would look something like:

public class List<T>
{
    public List()
    {
    }
}

The idea is that within the class declaration, T is already known - you only specify type parameters when you want to add more of them, e.g. for a generic method like ConvertAll.

Upvotes: 4

MortMan
MortMan

Reputation: 126

Should it not be more like:

public abstract class EventHandlerBase<TDetail> : EventHandlerBase    
  where TDetail : Contracts.DeliveryItemEventDetail
{
  public EventHandlerBase(AbstractRepository data, ILoggingManager loggingManager)
    : base(data, loggingManager)
  {
     // Initialize generic class
  }
}

Upvotes: 4

Related Questions