获取静态值 [英] Get value of static field
本文介绍了获取静态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下课程:
public static class Pages
{
public static string LoggedOut = "LoggedOut.aspx";
public static string Login = "Login.aspx";
public static string Home = "Home.aspx";
}
我知道我可以静态使用 Pages.Home
,但是有一个问题的原因.
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");
等
有可能吗?
谢谢,戴夫
推荐答案
您可以使用以下内容:
var field = typeof(Pages).GetField("Home", BindingFlags.Public | BindingFlags.Static);
var value = (string)field.GetValue(null);
这篇关于获取静态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文