C# 通过其字符串名称获取属性值 请考虑这个类: public static class Age { public static readonly string F1 = "18-25"; public static readonly string F2 = "26-35"; public static readonly string F3 = "36-45"; public static readonly string F4 = "46-55"; }

C# 通过其字符串名称获取属性值 请考虑这个类: public static class Age { public static readonly string F1 = "18-25"; public static readonly string F2 = "26-35"; public static readonly string F3 = "36-45"; public static readonly string F4 = "46-55"; },c#,c#-4.0,reflection,static,C#,C# 4.0,Reflection,Static,我想写一个得到“F1”并返回“18-25”的函数 private string GetValue(string PropertyName) .... 我该怎么做呢?您只需使用SWITCH语句即可执行上述任务: public static string GetValue(string PropertyName) { switch (PropertyName) { case "F1": return Age.F1; case

我想写一个得到“F1”并返回“18-25”的函数

private string GetValue(string PropertyName)
....

我该怎么做呢?

您只需使用
SWITCH
语句即可执行上述任务:

public static string GetValue(string PropertyName)
{
    switch (PropertyName)
    {
        case "F1":
            return Age.F1;
        case "F2":
            return Age.F2;
        case "F3":
            return Age.F3;
        case "F4":
            return Age.F4;
        default:
            return string.Empty;
    }
}
使用反射,可以执行以下操作:

public static string GetValueUsingReflection(string propertyName)
{
    var field = typeof(Age).GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
    var fieldValue = field != null ? (string)field.GetValue(null) : string.Empty;
    return fieldValue;
}

我做了一些测试,对于这种情况,这将起作用:

public static string GetValue(string PropertyName)
{
   return typeof(Age).GetField(PropertyName).GetValue(typeof(Age));
}
静态常数的工作原理似乎有点不同。但是上面的内容在OQ中对类有效

有关更一般的情况,请参阅


这就是反射的实现方式:

public static string GetValue(string PropertyName)
{
   return Age.GetType().GetProperty(PropertyName).ToString();
}
注意,GetProperty()可以返回null,如果传入“F9999”,则会导致崩溃

我还没有测试,您可能需要:

public static string GetValue(string PropertyName)
{
   return Age.GetType().GetProperty(PropertyName,BindingFlags.Static).ToString();
}

一般情况作为评论:

public static string GetValue(object obj, string PropertyName)
{
   return obj.GetType().GetProperty(PropertyName,BindingFlags.Static).ToString();
}
你应该使用这个类。 您可以通过
getType()
函数为正在使用的类获取一个。 在您拥有type use函数之后。 你会得到一门课。这个类有一个函数。 此值将返回属性的值。

尝试此操作并享受:

public static string GetValueUsingReflection(string propertyName)
    {
        var field = Type.GetType("Agenamespace" + "." + "Age").GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
        var fieldValue = field != null ? (string)field.GetValue(null) : string.Empty;
        return fieldValue;
    }

Agenamespace是Age类在其中声明的命名空间。

将反射与Linq一起使用:

 private string GetValue(string propertyName)
 {
       return typeof(Age).GetFields()
           .Where(field => field.Name.Equals(propertyName))
           .Select(field => field.GetValue(null) as string)
           .SingleOrDefault();
 }

谢谢,我可以将
类名
传递给此函数,并且不使用
年龄
?而是将对象作为第一个参数传递。谢谢,但有一个问题。我的类是
静态
,它没有
GetType()
方法。我的意思是这不正确:
Age.GetType()…
PropertyInfo.ToString()不返回属性值。@kerezo\ui发布您想要的内容。