Constraint cannot be special class System.Enum

本来兴高采烈实现了下面的代码。

private T str2enum<T>(string en) where T: Enum
        {
            var os = Enum.GetNames(typeof(T));

            foreach (var o in os)
            {
                var s = ((T)Enum.Parse(typeof(T), o));

                if (s.EnumToString() == en)
                {
                    return s;
                }
            }

            return default(T);
        }


VS突然提示
Error 1 Constraint cannot be special class ‘System.Enum’

突然感到莫名奇妙,于是查一查微软老大怎么解释的。

看到官方网上说了一堆也没太明白。反正就是不让用。于是很多人用了很多方法去绕过,比如用struct代替Enum绕过这个问题。

但是,很不巧,用了struct之后,Enum的扩展方法(public static class)就出不来了。晕死。

哎,放弃吧。

2 comments

  1. Franklyn Hollo 说道:

    I simply want to mention I’m beginner to weblog and seriously enjoyed you’re blog site. Likely I’m planning to bookmark your blog post . You surely come with beneficial articles. Appreciate it for sharing your webpage.

  2. Jannette Driesbach 说道:

    If you would like to increase your know-how simply keep visiting this web page and be updated with the most up-to-date information posted here.

发表评论

电子邮件地址不会被公开。