简体   繁体   English

获取静态字段的值

[英]Get value of static field

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. 我知道我可以静态地使用Pages.Home ,但我的问题有一个原因。

I wish to have a method that I can call like this: 我希望有一个方法,我可以像这样调用:

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

etc. 等等

C'est possible? C'est可能吗?

Thanks, Dave 谢谢,戴夫

You can use the following: 您可以使用以下内容:

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

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. 您当然应该根据实际需求调整错误处理。

Just my tuppence... if you are going to use literals ("Home"), then I would absolutely bind to the const , ie Pages.Home (they should probably be constants in the example given). 只是我的tuppence ...如果你打算使用文字(“Home”),那么我绝对会绑定到const ,即Pages.Home (它们应该是给定示例中的常量)。 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: 如果切换到const ,那么请注意它们表现为静态字段:

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. 如果它被大量使用,你也可以将它们缓存在字典中。

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];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM