【发布时间】:2021-09-29 17:47:50
【问题描述】:
我正在尝试使用选址值获取常量值,但我无法实现,您能帮我这样做吗
using System;
public static class Constants
{
public static class HostServer
{
public static string ABC = "abc.com";
}
public static class WMS
{
public const string URL = "/create/user";
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var x = Constants.GetType().GetProperty("ABC").GetValue(Constants, null).ToString();
Console.WriteLine(x);
}
}
提前致谢
【问题讨论】:
-
试试
var x = typeof(Constants).GetProperty("... -
问题是,为什么要使用反射?
-
var x = typeof(Constants).GetProperty("ABC").GetValue(Constants,null).ToString();我得到了以下错误'Constants' is a 'type' but is used like a 'variable' -
@DavidG 你能推荐一个上面的例子吗
-
我的意思是,你为什么不能直接使用
var x = Constants.HostServer.ABC;?
标签: c# .net visual-studio