获取静态字段的值

发布于 2024-08-03 01:02:50 字数 407 浏览 7 评论 0原文

我有以下课程:

public static class Pages
{
    public static string LoggedOut = "LoggedOut.aspx";
    public static string Login = "Login.aspx";
    public static string Home = "Home.aspx";
}

我知道我可以静态使用 Pages.Home ,但我的问题是有原因的。

我希望有一个可以像这样调用的方法:

string pageName = Pages.GetPage("Home");

等等。

这可能吗?

谢谢, 戴夫

I've got the following class:

public static class Pages
{
    public static string LoggedOut = "LoggedOut.aspx";
    public static string Login = "Login.aspx";
    public static string Home = "Home.aspx";
}

I know I can use Pages.Home statically, but there is a reason for my question.

I wish to have a method that I can call like this:

string pageName = Pages.GetPage("Home");

etc.

C'est possible?

Thanks,
Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情定在深秋 2024-08-10 01:02:50

您可以使用以下内容:

var field = typeof(Pages).GetField("Home", BindingFlags.Public | BindingFlags.Static);
var value = (string)field.GetValue(null);

You can use the following:

var field = typeof(Pages).GetField("Home", BindingFlags.Public | BindingFlags.Static);
var value = (string)field.GetValue(null);
晨光如昨 2024-08-10 01:02:50

您可以像康拉德建议的那样使用反射来做到这一点。但我认为使用字典而不是依赖反射来完成这样的任务是更好的设计。

public static class Pages
{
    private static readonly IDictionary<String, String> PageMap = null;

    private static Pages()
    {
        Pages.PageMap = new Dictionary<String, String>();

        Pages.PageMap.Add("LoggedOut", "LoggedOut.aspx");
        Pages.PageMap.Add("Login", "Login.aspx");
        Pages.PageMap.Add("Home", "Home.aspx");
    }

    public static GetPage(String pageCode)
    {
        String page;
        if (Pages.PageMap.TryGet(pageCode, out page)
        {
            return page;
        }
        else
        {
            throw new ArgumentException("Page code not found.");
        }
    }
}

当然,您应该根据您的实际要求调整错误处理。

You can do it like Konrad suggested using reflection. But I would consider it much better design to use a dictionary and not rely on reflection for such a task.

public static class Pages
{
    private static readonly IDictionary<String, String> PageMap = null;

    private static Pages()
    {
        Pages.PageMap = new Dictionary<String, String>();

        Pages.PageMap.Add("LoggedOut", "LoggedOut.aspx");
        Pages.PageMap.Add("Login", "Login.aspx");
        Pages.PageMap.Add("Home", "Home.aspx");
    }

    public static GetPage(String pageCode)
    {
        String page;
        if (Pages.PageMap.TryGet(pageCode, out page)
        {
            return page;
        }
        else
        {
            throw new ArgumentException("Page code not found.");
        }
    }
}

You should of course adjust the error handling to your actual requirements.

大姐,你呐 2024-08-10 01:02:50

只是我的注意...如果您要使用文字(“Home”),那么我绝对会绑定到 const,即 Pages.Home (它们可能应该是给定示例中的常量)。如果您有以下情况,反射方法可能会很方便:

string s = ...something clever...
string page = GetPage(s);

如果您确实切换到const,那么请注意它们表现为静态字段:

string s = ...something clever...
FieldInfo field = typeof(Pages).GetField(s,
     BindingFlags.Static | BindingFlags.Public);
string page = (string)field.GetValue(null);

如果大量使用它,您也可以缓存这些在字典里。

Just my tuppence... if you are going to use literals ("Home"), then I would absolutely bind to the const, i.e. Pages.Home (they should probably be constants in the example given). The reflection approach might be handy if you have:

string s = ...something clever...
string page = GetPage(s);

If you do switch to const, then note that they manifest as static fields:

string s = ...something clever...
FieldInfo field = typeof(Pages).GetField(s,
     BindingFlags.Static | BindingFlags.Public);
string page = (string)field.GetValue(null);

If it is used heavily you could also cache these in a dictionary.

薄情伤 2024-08-10 01:02:50

正如其他人所说,我会避免在这里使用反射。另外,虽然它添加了更多代码,但硬编码页面名称的枚举也是一个好主意(之前也建议过)

    public enum pages { LoggedOut, Login, Home }

    static Dictionary<pages, string> pageDict = new Dictionary<pages, string>() {
        {pages.Home, "Home.aspx"},
        {pages.Login, "Login.aspx"},
        {pages.LoggedOut, "LoggedOut.aspx"}
    };

    public static string getPage(pages pageName)
    {
        return pageDict[pageName];
    }

As other have said, I'd avoid using reflection here. Also, although it adds a bit more code, an enum for the hardcoded page names is also a good idea (also suggested previously)

    public enum pages { LoggedOut, Login, Home }

    static Dictionary<pages, string> pageDict = new Dictionary<pages, string>() {
        {pages.Home, "Home.aspx"},
        {pages.Login, "Login.aspx"},
        {pages.LoggedOut, "LoggedOut.aspx"}
    };

    public static string getPage(pages pageName)
    {
        return pageDict[pageName];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文