C# Generics won’t allow Delegate Type Constraints

A number of classes are unavailable as generic contraints – Enum being another.

For delegates, the closest you can get is “: class”, perhaps using reflection to check (for example, in the static constructor) that the T is a delegate:

static GenericCollection()
{
    if (!typeof(T).IsSubclassOf(typeof(Delegate)))
    {
        throw new InvalidOperationException(typeof(T).Name + " is not a delegate type");
    }
}

Leave a Comment