コード例 #1
0
        public static string GetText(this TokenKind kind)
        {
            // ToDo: SourceCodeGenerators would be better for this

            if (!kind.HasDefaultText())
            {
#if DEBUG
                var attr = typeof(TokenKind).GetMember(kind.ToString())
                           .FirstOrDefault(m => m.DeclaringType == typeof(TokenKind))
                           .GetCustomAttribute <DescriptionAttribute>();

                Debug.Assert(attr is null, $"TokenKind.{kind} should have HasDefaultText == true");
#endif
                return(null);
            }

            var attribute = typeof(TokenKind).GetMember(kind.ToString())
                            .FirstOrDefault(m => m.DeclaringType == typeof(TokenKind))
                            .GetCustomAttribute <DescriptionAttribute>();

            if (attribute is null)
            {
                throw new Exception(
                          $"TokenKind.{kind} should have a Description attribute with the default text");
            }

            return(attribute.Description);
        }