c# - 创建 URL 枚举 - 错误 : Identifer expected

标签 c# asp.net enums

被这个简单的问题羞辱了。出于某种原因,我想将所有 asp.net URL 放在一个枚举中。但我收到一个错误:identifer expected

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Admin.Code
{
    public enum url
    {
       /_layouts/Admin/test1.aspx,
       /_layouts/Admin/test2.aspx,
       /_layouts/Admin/test3.aspx
    }

    class AdminUrlSettings
    {
    }
}

谢谢。

最佳答案

这是我多次将枚举值转换为“友好字符串”的方法。您还可以使用它来创建“字符串值”枚举。它与 Msonic 的解决方案一脉相承,但该属性内置于 Framework 中。

public enum url
{
   [Description(@"/_layouts/Admin/test1.aspx")] Test1,
   [Description(@"/_layouts/Admin/test2.aspx")] Test2,
   [Description(@"/_layouts/Admin/test2.aspx")] Test3
}

...

public static string GetDescription(this Enum enumValue)
{
    object[] attr = enumValue.GetType().GetField(enumValue.ToString())
            .GetCustomAttributes(typeof (DescriptionAttribute), false);
        if (attr.Length > 0)
            return ((DescriptionAttribute) attr[0]).Description;

        return enumValue.ToString();
}

//usage
Response.Redirect(url.Test1.GetDescription());

关于c# - 创建 URL 枚举 - 错误 : Identifer expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10013715/

相关文章:

c# - 将标志转换为 IEnumerable<Enum> 的扩展方法,反之 (C#)

c# - How fix AddressOf requires a relaxed conversation to the delegate 错误

.NET 应用程序域和 ASP.NET

c# - 从一个位置调用多个 ASP.Net Core 微服务

c++ - 打开作用域枚举

mysql - 如何替换 MySQL 枚举值进行排序

node.js - Sequelize : Change column type to ENUM

c# - 使用 Blazor 进行客户端验证

c# - Crm 2011 实体集合查询和性能问题

asp.net - 系统NullReferenceException : Object reference not set to an instance of an object: