Anyone know a good workaround for the lack of an enum generic constraint?

后端 未结 12 1389
情书的邮戳
情书的邮戳 2020-11-22 15:37

What I want to do is something like this: I have enums with combined flagged values.

public static class EnumExtension
{
    public static bool IsSet

        
12条回答
  •  没有蜡笔的小新
    2020-11-22 15:55

    You can achieve this using IL Weaving and ExtraConstraints

    Allows you to write this code

    public class Sample
    {
        public void MethodWithDelegateConstraint<[DelegateConstraint] T> ()
        {        
        }
        public void MethodWithEnumConstraint<[EnumConstraint] T>()
        {
        }
    }
    

    What gets compiled

    public class Sample
    {
        public void MethodWithDelegateConstraint() where T: Delegate
        {
        }
    
        public void MethodWithEnumConstraint() where T: struct, Enum
        {
        }
    }
    

提交回复
热议问题