c# - 按属性名从列表中选择常量

标签 c# properties

<分区>

public class Constants
{
    public const string test1 = "This is testvalue1;"
    public const string test2 = "This is testvalue1;"
    public const string test3 = "This is testvalue1;"
    public const string test4 = "This is testvalue1;"
    public const string test5 = "This is testvalue1;"
}

是否可以通过使用 var propertyString = Constants.Where(<Propertyname> == test1).ToString() 以某种方式获取常量字符串? ?

最佳答案

你必须通过反射(reflection)来做到这一点。

string fieldName = "test1";
object fieldValue = typeof(Constants).GetField(fieldName, BindingFlags.Static).GetValue(null);

关于c# - 按属性名从列表中选择常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8988299/

相关文章:

javascript - 我可以在 React.js 中更新组件的 props 吗?

java - 逐行在属性文件中添加注释

java - 来自多个包的推土机通用映射(通配符?)

JavaScript:是否可以迭代您自己的变量?

javascript - 如何使用 'getOwnPropertyNames' 遍历 map 的内容

c# - 集合初始化语法,有什么用?

c# - c#中的数学模数

javascript - 如何在 ASP.NET Core MVC 中将 js 字符串发布到 C#

C# Dynamic Linq - 案例陈述

c# - 下载所有 Google 表格