C# Generics won't allow Delegate Type Constraints

前端 未结 8 1281
别跟我提以往
别跟我提以往 2020-11-28 08:01

Is it possible to define a class in C# such that

class GenericCollection : SomeBaseCollection where T : Delegate

I couldn

8条回答
  •  执念已碎
    2020-11-28 08:22

    As mentioned above, you cannot have Delegates and Enum as a generic constraint. System.Object and System.ValueType also cannot be used as a generic constraint.

    The work around can be if you construct an appropriate call in you IL. It will work fine.

    Here is a good example by Jon Skeet.

    http://code.google.com/p/unconstrained-melody/

    I have taken my references from Jon Skeet's book C# in Depth, 3rd edition.

提交回复
热议问题