Is it possible to define a class in C# such that
class GenericCollection : SomeBaseCollection where T : Delegate
I couldn
If you are willing to take a compile time dependency on an IL Weaver you can do this with Fody.
Using this addin to Fody https://github.com/Fody/ExtraConstraints
Your code can look like this
public class Sample
{
public void MethodWithDelegateConstraint<[DelegateConstraint] T> ()
{
}
public void MethodWithEnumConstraint<[EnumConstraint] T>()
{
}
}
And be compiled to this
public class Sample
{
public void MethodWithDelegateConstraint() where T: Delegate
{
}
public void MethodWithEnumConstraint() where T: struct, Enum
{
}
}